C++ C++;成员函数未声明错误,当它看起来是

C++ C++;成员函数未声明错误,当它看起来是,c++,function,private-members,function-declaration,C++,Function,Private Members,Function Declaration,我收到错误信息: cs163hw1.cpp:41:24:错误:在类“menutype”中未声明“int menutype::run_prog()”成员函数 及 main.cpp:18:7:错误:“struct menutype”没有名为“run\u prog”的成员 尝试使用相关代码(跨越appriprait.cpp和.h文件)编译我的程序时: int main(int argc,char**argv){ ... menu.run_prog(); ... 类菜单类型{ 公众: 菜单类型(int)

我收到错误信息:

cs163hw1.cpp:41:24:错误:在类“menutype”中未声明“int menutype::run_prog()”成员函数

main.cpp:18:7:错误:“struct menutype”没有名为“run\u prog”的成员

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

int main(int argc,char**argv){
...
menu.run_prog();
...
类菜单类型{
公众:
菜单类型(int);
int display();
int run_prog();
私人:
临时演员名单;
个人菜单;
};
int menutype::run_prog(){
bool exit=false;
int输入;
当(!退出){
输入=0;
while(输入<1 | |输入>4)
输入=显示();
开关(输入){
案例1:
打破
案例2:
打破
案例3:
打破
案例4:退出=真;
打破
违约:
打破
}
}
}

我不知道为什么会发生这种情况,有人猜测吗?

您需要在main()上面声明
类menutype
)。更好的做法是将类移动到其自己的专用文件menutype.cpp中,并在main的源文件中包含标头。如您所述,编译器在从文件顶部解析源文件时还不知道menutype。

我猜您正在调用
menu.run\u prog()
甚至在定义类之前。事实上,我打赌这就是问题所在。你能在帖子中为每个.cpp文件包含#include吗?还有你是否有一个包含保护(例如#ifdef宏)在您的头文件中,可能与另一个头文件冲突?@JamesBeilby,头保护是
\ifndef
@chris Typo代表我,您是正确的,当然通常情况下是这样。
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;
    }
}
}