我正在编译一个 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/