C ++错误:未定义对“ main”的引用 [英] C++ Error: undefined reference to `main'

查看:319
本文介绍了C ++错误:未定义对“ main”的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

list.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

这是list.cpp文件:

And here is the list.cpp file:

list.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];
};

我尝试了使用命名空间std以及如何包括在内的尝试,但我不知道找出如何使这些错误消失。

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?

推荐答案

对main()的未定义引用意味着您的程序缺少main()函数,这是强制性的适用于所有C ++程序。将此添加到某处:

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

int main()
{
  return 0;
}

这篇关于C ++错误:未定义对“ main”的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆