简体   繁体   English

VS2010:致命错误LNK1120:1个未解决的外部组件。 使用模板

[英]VS2010: fatal error LNK1120: 1 unresolved externals . Working with templates

Have a simple assignment with classes and templates for class, have searched on here and no solutions seem to fix the issue. 使用类和类的模板进行简单的分配,在此处进行了搜索,似乎没有解决方案可以解决此问题。 When I try to compile, this comes up: 当我尝试编译时,出现了:

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

Have a console project using an empty project. 有一个使用空项目的控制台项目。 Can anyone help me find the issue? 谁能帮我找到问题? Here is my code: 这是我的代码:

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

Your getting the error because you made main a template: 您收到错误是因为您将main设为模板:

template<typename ItemType>
int main()

Please remove the template<typename ItemType> . 请删除template<typename ItemType> main is not allowed to be a template. main不能作为模板。

Once you remove that, you will get errors on Vec3<double> v3 = v2; 删除后,您将在Vec3<double> v3 = v2;上看到错误Vec3<double> v3 = v2; because v2 is a Vec3<int> and cannot be converted to a Vec3<double> . 因为v2Vec3<int>且不能转换为Vec3<double>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM