简体   繁体   English

错误:****尚未声明

[英]Error : **** has not been declared

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 错误是:'Function'尚未声明
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: 在Function.h文件中:
I use 我用

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

instead of the old one 而不是旧的

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

All your include statements are missing the # : 您的所有include语句都缺少#

#include "Function.h"
^

Everything else looks fine, though you need to also #include <iostream> in Function.cpp since you're using cout . 其他一切看起来都很好,但是你需要在Function.cpp中使用#include <iostream> ,因为你正在使用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()
{
}

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. 我有两个头文件,包括彼此,它吐出这个错误。

In the first Function.h file you have declared the constructor but not defined it. 在第一个Function.h文件中,您已声明构造函数但未定义它。 In the second Function.h file (the one that works) you have defined and declared the Function constructor. 在第二个Function.h文件(有效的文件)中,您已经定义并声明了Function构造函数。 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.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). 你在头文件的第二个版本中做的另一件事是在“成员初始化列表”中初始化成员变量“status”,这是一件好事(参见Scott Meyers的Effective C ++,Item 4)。 Hope this helps :) 希望这可以帮助 :)

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;
}

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

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