C++成员函数未声明错误时(当它看起来是错误时)

C++ member function not declared error, when it appears to be

本文关键字:错误 看起来 未声明 成员 C++ 函数      更新时间:2023-10-16

我收到错误:

CS163hw1.cpp:41:24:错误:在类"菜单类型"中没有声明"int menutype::run_prog()"成员函数

主.cpp:18:7: 错误:"结构菜单类型"没有名为"run_prog"的成员

尝试使用关联的代码(跨越 appriprait .cpp 和 .h 文件)编译我的程序时:

int main(int argc, char ** argv){
...
menu.run_prog();
...
class menutype{
public:
  menutype(int);
  int display();
  int run_prog();
private:
  extras list;
  person menup;
};
int menutype::run_prog(){
bool exit = false;
int input;
while(!exit){
    input = 0;
    while(input < 1 || input > 4)
        input = display();
    switch(input){
        case 1 : 
            break;
        case 2 :
            break;
        case 3 :
            break;
        case 4 : exit = true;
            break;
        default :
            break;
    }
}
}

我不知道为什么会这样,有什么猜测吗?

你需要

在main()上面声明class menutype。 更好的方法是将类移动到它自己的专用文件中,称为menutype.cpp并将标头包含在main的源文件中。 正如您所描述的,编译器还不知道菜单类型,因为它从文件顶部解析源文件。

相关文章: