简体   繁体   English

“未在此范围内声明”错误的问题

[英]Problem with “not declared in this scope” error

I've got: 我有:

error a1 was not declared in this scope 在此范围内未声明错误a1

Can somebody please explain why this code causes that? 有人可以解释一下为什么此代码会导致这种情况吗?

quiz.h quiz.h

#ifndef QUIZ_H_
#define QUIZ_H_

    #include "quiz.cpp"   // I deleted this row
                          // and wrote void quiz(int i);


    class A {
    private:
        int player;
    public:
        A(int initPlayer);  // wrote here = 0
        ~A();
        void foo();
    };


    #endif /* QUIZ_H_ */

quiz.cpp quiz.cpp

#include "quiz.h"
#include <iostream>
using std::cout;
using std::endl;

A::A(int initPlayer = 0){    // deleted = 0
    player = initPlayer;
}

A::~A(){

}

void A::foo(){
    cout << player;
}

main function 主功能

#include "quiz.h"
int main()
{
        quiz(7);
        return 0;
}

quiz function 测验功能

#include "quiz.h"

void quiz(int i)
{
        A a1(i);
        a1.foo();
}

after my modifications I have an error multiple definition of quiz(int) 修改后,我有一个错误multiple definition of quiz(int)

不要#include quiz.cppquiz.h

You should not be including the .cpp file in the header. 您不应在标题中包含.cpp文件。 Remove: 去掉:

#include "quiz.cpp"

Also, the default value in: 另外,默认值在:

A::A(int initPlayer = 0){

should be in the header file instead. 应该在头文件中。

And edit your question provide the names of all your files, so we can tell you how to compile and link them. 并编辑您的问题,并提供所有文件的名称,以便我们告诉您如何编译和链接它们。

Never include a .cpp file. 切勿包含.cpp文件。 The header files are used to declare what functions will exist so the compiler knows that you will provide the implementation later (at linking time). 头文件用于声明将存在的功能,因此编译器知道您稍后将在链接时提供实现。 All an #include directive does is copy/paste the given file into the source. #include指令所做的只是将给定文件复制/粘贴到源文件中。 So in the header file, you've got the contents of your source file before the declarations. 因此,在头文件中,您需要在声明之前获得源文件的内容。 The only reason this didn't cause a recursive include was because of the #ifndef tag. 不会导致递归包含的唯一原因是由于#ifndef标记。

If you want to see what the #include tags are doing to your code, most C++ compilers allow you to only do the preprocessing stage. 如果要查看#include标记对代码的作用,大多数C ++编译器仅允许您执行预处理阶段。 For gcc, it's just this: 对于gcc,就是这样:

gcc -E main.cpp

And as someone else said, default values go in header files. 就像其他人所说的那样,默认值放在头文件中。

I think you have the includes in the wrong places. 我认为您在错误的位置包含了。

quiz.h should not be including the implementation of the class in quiz.cpp quiz.h不应在quiz.cpp中包括该类的实现

The implementation should be in quiz.cpp as you have For main to know about void quiz(int i) then you need to declare this in a header and include this header in main and the function file (or in this cas just put the function code in the same file as main and before it) 实现应在quiz.cpp中,因为您要使Main了解void quiz(int i)则需要在标头中声明此标头,并将此标头包含在main和函数文件中(或在cas中,只是将函数放入与main相同的文件中的代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM