【问题标题】:VS2010: fatal error LNK1120: 1 unresolved externals . Working with templatesVS2010: 致命错误 LNK1120: 1 unresolved externals 。使用模板
【发布时间】:2013-03-31 03:59:38
【问题描述】:

有一个包含类和类模板的简单作业,已在此处搜索,但似乎没有解决该问题的解决方案。当我尝试编译时,会出现:

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;
 }  

【问题讨论】:

  • 我得到一个 LNK 1120 未解决的外部错误,所以它不会编译
  • complete 错误是什么?您遗漏了描述问题所在的部分。
  • 你能说得更具体点吗?符号是什么?
  • 尝试编译,出现以下问题: 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 : 致命错误 LNK1120: 1 unresolved externals
  • “未解析的外部符号_main”表示缺少名为main 的非模板函数。

标签: c++ class templates unresolved-external


【解决方案1】:

您收到错误是因为您将main 设为模板:

template<typename ItemType>
int main()

请删除template&lt;typename ItemType&gt;main 不允许作为模板。

删除后,Vec3&lt;double&gt; v3 = v2; 将出现错误,因为 v2Vec3&lt;int&gt;,无法转换为 Vec3&lt;double&gt;

【讨论】:

  • 一旦所有相关信息都可用,答案会如何突然出现在你面前。
  • 是的,我读到它的第二个脑袋,谢谢你的回应!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 2013-12-15
  • 2011-11-16
  • 1970-01-01
相关资源
最近更新 更多