错误:****尚未声明

Xit*_*rum 2 c++

在我的Function.h文件中:

class Function{
  public:
    Function();
    int help();
};
Run Code Online (Sandbox Code Playgroud)

在我的Function.cpp文件中:

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

在我的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();
}
Run Code Online (Sandbox Code Playgroud)


错误是:'函数'尚未声明
可以有人告诉我为什么?谢谢.

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

class Function{
  public:
    int status;
    Function():status(1){}
    int help();
};
Run Code Online (Sandbox Code Playgroud)

而不是旧的

class Function{
  public:
    Function();
    int help();
};
Run Code Online (Sandbox Code Playgroud)

Cam*_*ron 7

您的所有include语句都缺少#:

#include "Function.h"
^
Run Code Online (Sandbox Code Playgroud)

其他一切看起来都很好,但是你需要#include <iostream>在Function.cpp中使用它cout.

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

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

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

Function::Function()
{
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我遇到了类似的问题.确保您只有所需的头文件.我有两个头文件,包括彼此,它吐出这个错误.