2012-12-16 42 views
1

我不斷收到以下兩個錯誤,我找不到原因。錯誤LNK2019:無法解析的外部符號主要在函數__tmainCRTStartup中引用MSVCRTD.lib(crtexe.obj)

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

error LNK1120: 1 unresolved externals 

確保我將它運行爲控制檯[甚至在鏈接屬性,它說控制檯] 我使用VC++ 2012旗艦版。我並非瘋狂地用專業術語和高級概念進行技術導向,所以請儘量不要壓倒我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 
+1

可能重複的[error LNK2019:無法解析的外部符號\ _main在函數中引用\ _ \ _ _ _ tmainCRTStartup](http://stackoverflow.com/questions/4845410/error-lnk2019-unresolved-external-symbol-main-參考功能tmainc) –

回答

3

在C++中的主要功能被聲明爲int main()int main(int argc, char *argv[])(參照C++標準,段落3.6.1主要功能)。

在你的情況下,鏈接器找不到main的主體,所以它會拋出一個錯誤。

+0

我現在感覺非常愚蠢T_T 我寫了main()很多次... –

+0

謝謝你的幫助。有時你只需要別人指出垃圾...... –

1

項目 - >屬性 - >配置屬性 - >鏈接器 - >系統

和不斷變化的子系統控制檯。

1

我並不完全確定,但通常這些錯誤大多依賴於您在創建項目時選擇的項目種類。如果你選擇控制檯,您通常需要遵循

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

,但如果你已經選擇的Windows類型的項目,那麼你就可以按照

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

但是,如果你正在修改_tmain到WinMain函數或反之亦然在一個已經存在的項目中,你可能想要做Arivazhagan一直暗示的。

您可以在屬性更改設置 - >配置屬性 - >鏈接器 - >系統到Windows,如果你想WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)格式

控制檯,如果你想_tmain(int argc, _TCHAR* argv[])格式。

相關問題