C++ 错误:**** 尚未声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5269283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:47:38  来源:igfitidea点击:

Error : **** has not been declared

c++

提问by Xitrum

In my Function.h file:

在我的 Function.h 文件中:

class Function{
  public:
    Function();
    int help();
};

In my Function.cpp file:

在我的 Function.cpp 文件中:

#include "Function.h"
int Function::help() //Error here
{
  using namespace std;
  cout << "Help";
  return 1;
}

In my Main.cpp

在我的 Main.cpp 中

#include <iostream>
#include "Function.h"
using namespace std;

int menu(){
  Function fc;
  fc.help();
  return 1;
}

int main(int args, char**argv){
  return menu();
}


Error is : ‘Function' has not been declared
Can anybody tell me why? Thank you.


错误是:“函数”尚未声明
谁能告诉我为什么?谢谢你。

I tried like this and the problem is solved, but I dont really understand why:
In Function.h file:
I use

我这样试过,问题解决了,但我真的不明白为什么:
在Function.h文件中:
我使用

class Function{
  public:
    int status;
    Function():status(1){}
    int help();
};

instead of the old one

而不是旧的

class Function{
  public:
    Function();
    int help();
};

回答by Cameron

All your include statements are missing the #:

您的所有包含语句都缺少#

#include "Function.h"
^

Everything else looks fine, though you need to also #include <iostream>in Function.cpp since you're using cout.

其他一切看起来都很好,尽管您还需要#include <iostream>在 Function.cpp 中,因为您使用的是cout.

Here is the Function.cpp that I got to compile and run:

这是我要编译和运行的 Function.cpp:

#include "Function.h"
#include <iostream>

int Function::help() // No error here
{
    using namespace std;
    cout << "Help";
    return 1;
}

Function::Function()
{
}

回答by Daniel

I had a similar problem. Make sure that you only have the required header files. I had two header files both including each other and it spit out this mistake.

我有一个类似的问题。确保您只有所需的头文件。我有两个相互包含的头文件,它吐出了这个错误。

回答by GWW

You have created a declaration for the constructor of the Function class without including it in your implementation (cpp file).

您已经为 Function 类的构造函数创建了一个声明,但没有将它包含在您的实现(cpp 文件)中。

#include "Function.h"

Function::Function(){
    // construction stuff here
}

int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}

回答by Jake_Howard

In the first Function.h file you have declared the constructor but not defined it. In the second Function.h file (the one that works) you have defined and declared the Function constructor. You can either define and declare in the header or file, or declare in the header file and define in the Function.cpp file.

在第一个 Function.h 文件中,您已经声明了构造函数但没有定义它。在第二个 Function.h 文件(有效的文件)中,您已经定义并声明了 Function 构造函数。您可以在头文件或文件中定义并声明,也可以在头文件中声明并在Function.cpp 文件中定义。

For example, declare in the header file "Function.h":

例如在头文件“Function.h”中声明:

    class Function
    {
    Function();
    }

and define here in "Function.cpp":

并在“Function.cpp”中定义:

    Function::Function(){}

Or the alternative is to declare and define in the header file "Function.h":

或者另一种方法是在头文件“Function.h”中声明和定义:

    Class Function
    {
    Function(){}
    }

The other thing that you have done in the second version of the header file is to initialise the member variable "status" in the "member initialisation list" which is a good thing to do (See Effective C++ by Scott Meyers, Item 4). Hope this helps :)

您在头文件的第二个版本中所做的另一件事是在“成员初始化列表”中初始化成员变量“状态”,这是一件好事(请参阅 Scott Meyers 的 Effective C++,第 4 项)。希望这可以帮助 :)