Я не могу перегрузить оператора +, и я понятия не имею, почему. Я пробовал много решений, но мне не удалось решить эту проблему. (Оператор + = отлично работает)Перегрузка оператора «+» в файле .h, ошибка компоновщика
Я не понимаю. Не могли бы вы помочь?
Я получаю сообщение об ошибке:
LNK2019: unresolved external symbol "public: __thiscall Zbior::Zbior(class Zbior const &)" ([email protected]@@[email protected]@@Z) referenced in function "public: class Zbior __thiscall Zbior::operator+(class Zbior const &)" ([email protected]@@[email protected]@@Z)
Проблема не существует, когда я изменить возвращаемый объект метода: оператора Zbior + (сопзИте Zbior & другого)
к
return Zbior<T>();
Мой файл .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;
};
И мой файл .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;
}
Ussage класса:
#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;
}
Это не дубликат. Я это видел. Мой код не работает и просто не знает, почему. – user3108729
ЕГО дубликат. Я не понимаю, почему вы пишете файлы .cpp с помощью шаблонов, если вы действительно прочитали этот поток ... – Manu343726
@juanchopanza Не совсем обманутый, так как он '# включает' файл cpp, но ошибки могут быть из-за того, что он включает он перед файлом h – Praetorian