C++ 错误:对“main”的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14620435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:31:19  来源:igfitidea点击:

C++ Error: undefined reference to `main'

c++

提问by smileham

I'm working on a simple class List, but when compiling the header and cpp file, I get the error: undefined reference to `main'

我正在处理一个简单的类 List,但是在编译头文件和 cpp 文件时,出现错误:未定义对 `main' 的引用

What am I doing wrong, and how could I fix this?

我做错了什么,我该如何解决?

Here is the list.h file that has simple headers:

这是具有简单标题的 list.h 文件:

list.h

列表.h

#ifndef LIST_H
#define LIST_H

#include <string>

const int DEFAULT_CAPACITY = 100;

class List
{
    public:
        List();
        List(int capacity);
        ~List();
        void push_back(std::string s);
        int size() const;
        std::string at(int index) const;

    private:
        std::string* mData;
        int mSize;
        int mCapacity;
};

#endif

And here is the list.cpp file:

这是 list.cpp 文件:

list.cpp

列表.cpp

#include "list.h"
#include <string>

List::List(){
    mData = new std::string[DEFAULT_CAPACITY];
    mSize = 0;
    mCapacity = 100;
};

List::List(int capacity){
    mData = new std::string[capacity];
    mSize = 0;
    mCapacity = capacity;
};

List::~List(){
    delete[] mData;
};

void List::push_back(std::string s){
    if (mSize<mCapacity){
        mData[mSize] = s;
        mSize++;
    }
};

int List::size() const{
    return mSize;
};

std::string List::at(int index) const{
    return mData[index];
};

I tried experimenting around with "using namespace std" and how to include , but I can't figure out how to get these errors to go away. What is causing them?

我尝试尝试使用“使用命名空间 std”以及如何包含,但我不知道如何消除这些错误。是什么导致了它们?

采纳答案by Lundin

Undefined reference to main() means that your program lacks a main() function, which is mandatory for all C++ programs. Add this somewhere:

对 main() 的未定义引用意味着您的程序缺少 main() 函数,这对于所有 C++ 程序都是必需的。在某处添加这个:

int main()
{
  return 0;
}

回答by Keith Thompson

You should be able to compilelist.cpp, you can't linkit unless you have a main program. (That might be a slight oversimplification.)

你应该能够编译list.cpp,除非你有一个主程序,否则你不能链接它。(这可能有点过于简单化了。)

The way to compile a source file without linking it depends on what compiler you're using. If you're using g++, the command would be:

在不链接的情况下编译源文件的方法取决于您使用的编译器。如果您正在使用g++,命令将是:

g++ -c list.cpp

That will generate an object file containing the machine code for your class. Depending on your compiler and OS, it might be called list.oor list.obj.

这将生成一个包含您的类的机器代码的目标文件。根据您的编译器和操作系统,它可能被称为list.olist.obj

If you instead try:

如果您改为尝试:

g++ list.cpp

it will assume that you've defined a mainfunction and try to generate an executable, resulting in the error you've seen (because you haven'tdefined a mainfunction).

它将假设您已经定义了一个main函数并尝试生成一个可执行文件,从而导致您看到的错误(因为您还没有定义一个main函数)。

At some point, of course, you'll need a program that uses your class. To do that, you'll need another .cppsource file that has a #include "list.h"and a main()function. You can compile that source file and link the resulting object together with the object generated from list.cppto generate a working executable. With g++, you can do that in one step, for example:

当然,在某些时候,您将需要一个使用您的类的程序。为此,您需要另一个.cpp具有 a#include "list.h"和 amain()函数的源文件。您可以编译该源文件并将生成的对象与生成的对象链接在一起list.cpp以生成工作可执行文件。使用g++,您可以一步完成,例如:

g++ list.cpp main.cpp -o main

You have to have a mainfunction somewhere. It doesn't necessarily have to be in list.cpp. And as a matter of style and code organization, it probably shouldn't be in list.cpp; you might want to be able to use that class from more than one main program.

你必须在main某处有一个功能。它不一定必须在list.cpp. 作为风格和代码组织的问题,它可能不应该在list.cpp; 您可能希望能够从多个主程序中使用该类。