c++ - 错误: "Undefined reference to ' main'"

标签 c++ linux

我正在编译一个 C++ 程序,但我不断收到毫无意义的代码行,然后在其底部显示“对‘菜单’的 undefined reference ”。我有一个 .h 文件和一个 .cpp 文件,菜单功能在我的 .h 文件中定义,在我的 .cpp 文件中,我在顶部包含我的 .h 文件,这也是我实现菜单功能的地方。是的,我正在同时编译它们

头文件
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;

/*
struct dog_park
{
   char * name;
   char * location;
   char * description;
   char * fence;
   char * size;

};
*/
class parks
{

   public:
      struct dog_park
      {
         char * name;
         char * location;
         char * description;
         char * fence;
         char * size;
      };
      parks();
      int menu();
      bool display_all();
      void add_park();
      bool search_park();
      ~parks();

   private:
      dog_park * all_parks;
      int length;


};
.cpp 文件
//implementation of functions 

#include "cs162_parks.h"

parks::parks()
{
   all_parks = new dog_park[length];   

}

//allows for user to select what action to take
int parks::menu()
{
   int choice = 0;
   cout << "Welcome to the menu, your choices to choose from are: " << endl << endl;
   cout << "1. Add a dog park to list" << endl;
   cout << "2. Search for specific park by name" << endl;
   cout << "3. Display all dog parks" << endl;
   cout << "4. Quit" << endl << endl;
   cout << "What menu selection do you choose? (1-4): ";
   cin >> choice;
   cin.ignore(100, '\n');

   return choice;
}

parks::~parks()
{
   if (all_parks)
      delete [] all_parks;
}

最佳答案

每个 C++ 程序(对于普通的托管实现)都需要一个 main 函数;它可以看起来像这样:

int main()
{
    // Top level statements
}

main在程序运行时自动调用。

有些事情,准备工作,必须在main之前完成。这些甚至包括程序员指定的东西。因此,main 的调用并不是程序中第一次发生,即 main 不是程序的机器代码级别入口点,但它是主要的事情。

关于c++ - 错误: "Undefined reference to ' main'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37557459/

相关文章:

python - 从终端捕获结果(外部进程)

c++ - 返回对特定大小数组的引用,而不在返回类型中明确说明大小

c++ - 检索函数内用 operator new 分配的类指针对象成员的值的问题

c++ - 几次访问后,mmap 因无法打开/dev/mem 而死

c++ - mov bl 在汇编中做了什么

linux - 用于运行第二个 mysql 服务器的 init.d 脚本失败

c++ - gcc 本地安装在主目录中

c++ - 在分块矩阵中查找一个值

linux - 如何编写 shell 脚本来复制脚本所在目录中的文件

c - C语言代码如何在linux中设置路径环境变量