错误:"Undefined reference to 'main'"

Error: "Undefined reference to 'main'"

本文关键字:main reference Undefined 错误 to      更新时间:2023-10-16

我正在编译一个c++程序,但我不断得到毫无意义的代码行,然后在它的底部写着"对‘菜单’的未定义引用"。我有一个.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不是程序的机器代码级入口点,但它是最重要的。