C++ 重载'+';.h文件中的运算符,链接器错误

C++ 重载'+';.h文件中的运算符,链接器错误,c++,operator-overloading,linker-errors,C++,Operator Overloading,Linker Errors,我无法使+运算符过载,我不知道为什么。 我尝试了很多解决方案,但都没能解决这个问题。 (操作员+=工作正常) 我不明白。你能帮忙吗 我得到一个错误: LNK2019:未解析的外部符号“public:\u thiscall” Zbior::Zbior(类Zbior常量&)“ (??0?$Zbior@H@@QAE@ABV0@@Z) 在函数“public:class”中引用 Zbior uu thiscall Zbior::operator+(类Zbior const&) (??H$Zbior@H@@

我无法使+运算符过载,我不知道为什么。 我尝试了很多解决方案,但都没能解决这个问题。 (操作员+=工作正常)

我不明白。你能帮忙吗

我得到一个错误:

LNK2019:未解析的外部符号“public:\u thiscall” Zbior::Zbior(类Zbior常量&)“ (??0?$Zbior@H@@QAE@ABV0@@Z) 在函数“public:class”中引用 Zbior uu thiscall Zbior::operator+(类Zbior const&) (??H$Zbior@H@@QAE?AV0@ABV0@@Z)

更改方法的返回对象时不存在问题: Zbior运算符+(常数Zbior和其他)

返回Zbior();
我的.h文件是:

template<class T>
class Zbior
{
public:
    Zbior(void);
    ~Zbior(void);
    void dodajElement(T elem);
    Zbior<T>(vector<T> elementyZbioru);
    Zbior<T>(Zbior<T> const &other);     
    Zbior<T>& operator+=(Zbior<T> const &other);

    Zbior<T>  operator+ (const Zbior<T> &other)
    {
        vector<T> otherVec = other.elementyZbioru;
        vector<T> thisVec = elementyZbioru;
        Zbior<T> wyjZbior = Zbior<T>();
        for (vector<T>::iterator p = otherVec.begin(); p != otherVec.end(); ++p)
        {
            wyjZbior.dodajElement(*p);
        }
        for (vector<T>::iterator p = thisVec.begin(); p != thisVec.end(); ++p)
        {
            wyjZbior.dodajElement(*p);
        }


        return Zbior<T>(wyjZbior);
    }
private:
    vector<T> elementyZbioru;

};
模板
类Zbior
{
公众:
Zbior(无效);
~Zbior(无效);
无效元素(T元素);
Zbior(向量元素zbioru);
Zbior(Zbior常数和其他);
Zbior和运算符+=(Zbior常量和其他);
Zbior运算符+(常数Zbior和其他)
{
vector otherVec=other.elementyZbioru;
向量thisVec=elementyZbioru;
Zbior wyjZbior=Zbior();
for(vector::iterator p=otherVec.begin();p!=otherVec.end();++p)
{
wyjZbior.dodaj元素(*p);
}
对于(向量::迭代器p=thisVec.begin();p!=thisVec.end();++p)
{
wyjZbior.dodaj元素(*p);
}
返回Zbior(wyjZbior);
}
私人:
向量元素;
};
我的.cpp文件是:

#include "Zbior.h"

template<class T>
Zbior<T>::Zbior(void)
{
}

template<class T>
Zbior<T>::~Zbior(void)
{
}

template<class T>
Zbior<T>::Zbior(vector<T> elementyZbioru)
{
    this->elementyZbioru = elementyZbioru;
}

template<class T>
void Zbior<T>::dodajElement(T  elem){
    this->elementyZbioru.push_back(elem);
}
template<class T>
Zbior<T>& Zbior<T>::operator+=(Zbior<T> const &inny){

    vector<T> innyElementyZbioru = (inny.elementyZbioru);

    for (vector<T>::iterator p = innyElementyZbioru.begin(); p != innyElementyZbioru.end(); ++p)
    {
        dodajElement(*p);
    }
    return *this;
}
#包括“Zbior.h”
模板
Zbior::Zbior(void)
{
}
模板
Zbior::~Zbior(无效)
{
}
模板
Zbior::Zbior(向量元素zbioru)
{
此->elementyZbioru=elementyZbioru;
}
模板
void Zbior::dodaj元素(T元素){
此->元素向后推(元素);
}
模板
Zbior&Zbior::运算符+=(Zbior常量&inny){
向量innyElementyZbioru=(inny.elementyZbioru);
对于(向量::迭代器p=innyElementyZbioru.begin();p!=innyElementyZbioru.end();+p)
{
dodaj元素(*p);
}
归还*这个;
}
班级年龄:

#include "stdafx.h"
#include "Zbior.cpp"
#include "Zbior.h"


int _tmain(int argc, _TCHAR* argv[])
{
    Zbior<int> zb1 = Zbior < int>();
    zb1.dodajElement(1);
    zb1.dodajElement(1312);



    Zbior<int> zb2 = Zbior < int>();
    zb2.dodajElement(21);
    zb2.dodajElement(21312);

    //zb1 += zb2;
    Zbior<int> zb = zb1 + zb2;


    //Zbior<Zbior<int>> zbzb = Zbior<Zbior<int>> ();
    //zbzb.dodajElement(zb1);
    //zbzb.dodajElement(zb2);

    system("PAUSE");
    return 0;
}
#包括“stdafx.h”
#包括“Zbior.cpp”
#包括“Zbior.h”
int _tmain(int argc,_TCHAR*argv[]
{
Zbior zb1=Zbior();
zb1.多达杰元素(1);
zb1.多达杰元素(1312);
Zbior zb2=Zbior();
zb2.多达杰元素(21);
zb2.dodajElement(21312);
//zb1+=zb2;
Zbior zb=zb1+zb2;
//Zbior zbzb=Zbior();
//zb.dodaj元素(zb1);
//zb.dodaj元素(zb2);
系统(“暂停”);
返回0;
}

您声明了一个副本构造函数

Zbior<T>(Zbior<T> const &other);
Zbior(Zbior常量和其他);
但不要在任何地方定义它。由于该类不包含任何在复制时需要特别注意的成员,因此只需删除该声明—隐式生成的复制构造函数和赋值运算符将执行正确的操作

(如果对包含模板实现的头使用的文件扩展名不是
.cpp
,可能会更好,因为这会导致包括我在内的一些人认为错误是将实现放在源文件中。或者只是将实现放在与类模板定义相同的头中。)

一个“未解决的外部错误”意味着您已经声明并尝试使用某个函数,但尚未提供该函数的定义。有点像这样:

int foo();

int main()
{
  int n = foo();
  return n;
}
foo
已经被声明和调用(在
main
中),但是没有实现它

你可以实现这个构造函数,或者因为你不需要任何特殊的行为,只需要删除声明


请再次查看您的错误消息:

LNK2019:未解析的外部符号“公共:此调用” Zbior::Zbior(类Zbior常量&)“(?0$Zbior@H@@QAE@ABV0@@Z) 在函数“public:class Zbior\uu thiscall”中引用 Zbior::运算符+(类Zbior常量&)“(??H$Zbior@H@@QAE?AV0@ABV0@@Z)

这表明您已声明:

template<class T>
class Zbior
{
public:
  [...]
    Zbior<T>(Zbior<T> const &other);    
模板
类Zbior
{
公众:
[...]
Zbior(Zbior常数和其他);
但没有定义它


事实上,当我查看你发布的其他代码时,我发现这是真的。这个复制构造函数没有定义。

它不是一个副本。我看到了这一点。我的代码不工作,只是不知道为什么。它是一个副本。我不明白如果你真的读过那个线程,为什么你要用模板编写.cpp文件…@juanc由于hopanza正在
#include
处理cpp文件,因此他并不是一个傻瓜,但错误可能是因为他在h文件之前包含了该文件。错误是因为您没有定义复制构造函数,只是在类定义中声明了它。在这种情况下,您可以删除该声明。此外,您还缺少
typename
where ver您指的是
vector::iterator
。如果您知道在使用类时必须对编译器可见,那么在头文件和cpp文件之间分离类定义有什么意义呢?
。inl
.h
的传统名称,包含实现(
inl
意思是内联)。或者干脆称它为
foo_impl.h
,我更喜欢它。谢谢!我浪费了4个小时测试了许多不同的解决方案,但忘记了定义它…(没有复制构造函数,它仍然崩溃,这是必需的)
template<class T>
class Zbior
{
public:
  [...]
    Zbior<T>(Zbior<T> const &other);