错误LNK2019:函数__tmainCRTStartup中引用的未解析的外部符号main MSVCRTD.lib(crtexe.obj)

时间:2012-12-16 20:17:27

标签: c++ templates visual-c++ main lnk2019

我一直收到以下两个错误,我无法弄清楚原因。

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib(crtexe.obj)

error LNK1120: 1 unresolved externals

确保我将其作为控制台运行[即使在链接器属性中它表示控制台] 我正在使用VC ++ 2012 Ultimate。我不是疯狂地用技术和高级概念进行技术导向,所以请尽量不要压倒我Dx。

//Main.cpp
#include <conio.h>
#include "XArray.h"


int Main() {
    XArray<int> Test;
    Test.Add(3);
    getch();
    return 1;
}

随后。

#ifndef XARRAY_H
#define XARRAY_H

template <class X>
class XIndex {
public:
    X Value;
    XIndex<X> *Next;

    //Construct
    XIndex(X ArrayValue) { Value = ArrayValue; Next = nullptr; }

};

template <class X>
class XArray {
    XIndex<X> *First;
public:
    //Construct/Destruct;
    XArray() { First = nullptr; }
    ~XArray();

    //Operators that Alter the structure;
    void Add(X); //Add X to the end.
    void AddX(X, int); //Create Int amount of X values
    void Insert(int, X); //Insert X at Index int
    bool Remove(int); //Remove Index int. Return true if deleted, false if failed.
    //void Sort(); //Todo -- Sorts by value.
    //Operators that deal with the values.
    X Get(int);
    void Set(int, X);


};

template <class X>
void XArray<X>::AddX(X NewVal, int Quantity) {
    for (int i = 0; i < Quantity; i++)
        Add(NewVal);
}

template <class X>
void XArray<X>::Add(X NewVal) {
    XIndex<X> *CurrentIndex;
    XIndex<X> *NewNode; //Where we store NewVal
    NewNode = new XIndex<X>(NewVal);

    //List doesn't exist.
    if (!First) { 
        First = NewNode;
    } else {
        //Start at beginning.
        CurrentIndex = First;
        while ((*CurrentIndex).Next){ 
            CurrentIndex = (*CurrentIndex).Next;
        }
        (*CurrentIndex).Next = NewNode;
    }

}

template <class X>
void XArray<X>::Insert(int Index, X NewVal) {
    XIndex<X> *CurrentIndex;
    XIndex<X> *PrevIndex;
    XIndex<X> *NewNode; //Where we store NewVal
    iCounter = 0;
    NewNode = new XIndex<X>(NewVal);

    //List doesn't exist.
    if (!First) { 
        First = NewNode;
    } else {
        //Start at beginning.
        CurrentIndex = First;

        while ((*CurrentIndex).Next != nullptr && iCounter < Index){ 
            iCounter += 1
            PrevIndex = CurrentIndex;
            CurrentIndex = (*CurrentIndex).Next;
        }
        if (PreviousNode == nullptr) {
            First = NewNode;
            (*NewNode).Next = CurrentIndex;
        } else {
            (*PrevIndex).Next = NewNode;
            (*NewNode).Next = CurrentIndex;
        }
    }

}

template <class X>
bool XArray<X>::Remove(int Index) {
    XIndex<X> *CurrentIndex;
    XIndex<X> *PrevIndex;
    int iCounter = 0;
    if (First == nullptr) return false; //We never had a list.

    //It's the first one;
    if (Index == 0) {
        CurrentIndex = (*First).Next;
        delete First;
        First = CurrentIndex;
        return true;
    }

    //Starting as normal.
    CurrentIndex = First;

    //Loop until we reach the index.
    while (CurrentIndex != nullptr && iCounter < Index) {
        iCounter += 1
        PrevIndex = CurrentIndex;
        CurrentIndex = (*CurrentIndex).Next;
    }
    if (CurrentIndex != nullptr) {
        (*PrevIndex).Next = (*CurrentIndex).Next;
        delete CurrentIndex;
        return true;
    }
    return false;

}

//Destructor;
template <class X>
XArray<X>::~XArray() {
    XIndex<X> *NextIndex;
    XIndex<X> *CurrentIndex;

    //Start at beginning.
    CurrentIndex = First;

    //So long as we aren't at the end [when Next = nullptr];
    while (CurrentIndex != nullptr) {
        NextIndex = (*CurrentIndex).Next;
        delete CurrentIndex;
        CurrentIndex = NextIndex;
    }
}
#endif

3 个答案:

答案 0 :(得分:3)

在C ++中,main函数声明为int main()int main(int argc, char *argv[])(参见C ++标准,第3.6.1节主函数)。

在您的情况下,链接器找不到main的正文,因此会抛出错误。

答案 1 :(得分:2)

项目 - &gt;属性 - &gt;配置属性 - &gt;链接器 - &gt;系统

并将SubSystem更改为Console。

答案 2 :(得分:1)

我不完全确定,但通常这些错误主要取决于您在创建项目时选择的项目类型。如果选择Console,通常需要按照

int _tmain(int argc, _TCHAR* argv[])

但是如果你选择了Windows类型的项目,那么你可以按照

int WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)

但是如果你在现有的项目中将_tmain修改为WinMain,反之亦然,那么你可能想要做Arivazhagan一直在暗示的事情。

您可以更改属性中的设置 - &gt;配置属性 - &gt;链接器 - &gt;如果您想要WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)格式

,则系统到Windows

如果您想要_tmain(int argc, _TCHAR* argv[])格式,请使用控制台。