2013-04-08 71 views
0

使用類和模板進行類的簡單分配,已經在這裏搜索,沒有解決方案似乎可以解決問題。當我嘗試編譯時,出現:VS2010:致命錯誤LNK1120:1個未解決的外部事件。使用模板

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup 1>c:\users\leanne\documents\visual studio 2010\Projects\PartC\Debug\PartC.exe : fatal error LNK1120: 1 unresolved externals 

有一個使用空項目的控制檯項目。任何人都可以幫我找到問題嗎? 這裏是我的代碼:

#include <iostream> 
using namespace std; 
template<typename ItemType> 
class Vec3 
{ 
    ItemType x, y ,z; 
public: 
    Vec3<ItemType>(){x=0;y=0;z=0;} 
    Vec3<ItemType>(const Vec3<ItemType>& other); 
    Vec3<ItemType> operator+(Vec3<ItemType>); 
    Vec3<ItemType> operator-(Vec3<ItemType>); 
    bool operator==(Vec3<ItemType> other); 
    Vec3<ItemType> operator=(Vec3<ItemType>); 
    ~Vec3<ItemType>(){;} 
}; 
template<typename ItemType> 
Vec3<ItemType> Vec3<ItemType>::operator+(Vec3<ItemType> other) 
{ 
    Vec3 temp; 
    temp.x = x + other.x; 
    temp.y = y + other.y; 
    temp.z = z + other.z; 
    return temp; 
} 
template<typename ItemType> 
bool Vec3<ItemType>::operator==(Vec3<ItemType> other) 
{ 
    if(x != other.x) 
     return false; 
    if(y != other.y) 
     return false; 
    if(z != other.z) 
     return false; 
    return true; 
} 
template<typename ItemType> 
Vec3<ItemType> Vec3<ItemType>::operator-(Vec3<ItemType> other) 
{ 
    Vec3 temp; 
    temp.x = x - other.x; 
    temp.y = y - other.y; 
    temp.z = z - other.z; 
    return temp; 
} 
template<typename ItemType> 
Vec3<ItemType> Vec3<ItemType>::operator=(Vec3<ItemType> other) 
{ 
    x = other.x; 
    y = other.y; 
    z = other.z; 
    return *this; 
} 
template<typename ItemType> 
Vec3<ItemType>::Vec3(const Vec3<ItemType>& other) 
{ 
    x = other.x; 
    y = other.y; 
    z= other.z; 
} 
template<typename ItemType> 
int main() 
{ 
    Vec3<int> v1; 
    Vec3<int> v2(v1); 
    Vec3<double> v3 = v2; 
    v3 = v1+v2; 
    v3 = v1-v2; 
    if(v1==v2) 
    { 
     return 0; 
    } 
    return 0; 
} 
+1

*問題*是什麼? – 2013-04-08 22:01:32

+0

即時獲取LNK 1120無法解析的外部錯誤,所以它不會編譯 – 2013-04-08 22:03:29

+0

什麼是*完整*錯誤?你遺漏了描述錯誤的部分。 – 2013-04-08 22:04:14

回答

1

你得到的錯誤,因爲你做main模板:

template<typename ItemType> 
int main() 

請刪除template<typename ItemType>main不允許爲模板。

刪除之後,您將收到Vec3<double> v3 = v2;的錯誤消息,因爲v2Vec3<int>,因此無法將其轉換爲Vec3<double>

+1

一旦所有相關信息都可用,驚人的答案就會跳出來。 – 2013-04-08 22:11:30

+0

是的,第二次我讀了它,謝謝你的迴應! – 2013-04-08 22:12:16

相關問題