我在从另一个类调用主函数时遇到问题

I'm having trouble calling a function in my main from another class

本文关键字:函数 遇到 问题 调用 另一个      更新时间:2023-10-16

我有一个主类,我试图在其中调用一个函数来创建菜单,但我不断收到此错误:

错误 LNK2019:函数 _main 中引用的未解析外部符号"public: static int __cdecl Controller::menu(void)"(?menu@Controller@@SAHXZ

这是我的主要课程。

#include "Main.h"
using namespace std;
int main () 
{
Control:: menu();
return 0;
}

这是主。

#pragma once
#include "Control.h"
class Main:
{
public:
Main(void);
~Main(void);
int main();
};

控制.h:

#pragma once
#include <iostream>
class Control
{
public:
Control(void);
~Control(void);
 static int menu ();
};

最后是控制 cpp 文件:

#include "Control.h"
using namespace std; 
static int menu () 
{
  bunch of menu code
 return 0;
}

我认为这很简单,但我就是想不通。我尝试删除静态并将函数更改为空函数,但都没有奏效。

static int menu () 
{
  bunch of menu code
 return 0;
}

应该是

int Control::menu () 
{
  bunch of menu code
 return 0;
}

这是定义成员的正确方法。

静态函数及其原型应该是这样的。

int Control :: menu()
{
   //bunch of menu code
   return 0 ;
}

在另一个文件中实现类时,还必须将类名与范围解析运算符一起使用。

您在类 Main 的末尾还有一个额外的冒号,从而导致语法错误。

相关文章: