-2

I am not able to overload the + operator and I have no idea why. I've tried a lot of solutions but I didn't manage to solve this problem. (Operator += works fine)

I don't get it. Could you help?

I get error:

LNK2019: unresolved external symbol "public: __thiscall Zbior::Zbior(class Zbior const &)" (??0?$Zbior@H@@QAE@ABV0@@Z) referenced in function "public: class Zbior __thiscall Zbior::operator+(class Zbior const &)" (??H?$Zbior@H@@QAE?AV0@ABV0@@Z)

The problem does not exist when I change the return object of method: Zbior operator+ (const Zbior &other)

to

return Zbior<T>();

My .h file is:

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;

};

And my .cpp file is:

#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 of the class:

#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;
}
Zong
  • 6,160
  • 5
  • 32
  • 46
  • It is not a dublicate. I've seen this. My code doesn't work and just don't know why. – user3108729 Dec 16 '13 at 21:05
  • ITS a duplicate. I don't understand why are you writting .cpp files with templates if you really have readed that thread... – Manu343726 Dec 16 '13 at 21:06
  • @juanchopanza Not quite a dupe since he's `#include`ing the cpp file, but the errors might be because he's including it before the h file – Praetorian Dec 16 '13 at 21:07
  • The error is because you haven't defined the copy constructor, only declared it within the class definition. You can just delete the declaration in this case. Also you're missing `typename` wherever you refer to `vector::iterator`. And what's the point of separating the class definition between header and cpp files if you know the definitions must be visible to the compiler when you use the class? – Praetorian Dec 16 '13 at 21:13

2 Answers2

3

You declare a copy constructor

Zbior<T>(Zbior<T> const &other);

but don't define it anywhere. Since the class doesn't contain any members that need special attention when copying, just delete that declaration - the implicitly generated copy constructor and assignment operator will do the right thing.

(It might be better if you used a file extension other than .cpp for the header containing the template's implementation, since that lead several people, including myself, to think that the error was putting the implementation in a source file. Or just put the implementation in the same header as the class template definition.)

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    `.inl` is a traditional name for `.h`-containing-implementations (`inl` meaning inline). Or just call it `foo_impl.h`, which I prefer. – Yakk - Adam Nevraumont Dec 16 '13 at 21:17
  • Thank u! I wasted 4hours on testing many different solutions but forget about definig it... (Without the copy constructor it still crashes, it is needed) – user3108729 Dec 16 '13 at 21:20
2

An "unresolved external error" means that you have declared and tried to use some function, but you haven't provided a definition for that function. Kinda like this:

int foo();

int main()
{
  int n = foo();
  return n;
}

foo has been declared and called (in main), but there's no implementation for it.

You can either implement this constructor, or since you don't need any special behavior within it, just delete the declaration.


Take another look at your error message:

LNK2019: unresolved external symbol "public: __thiscall Zbior::Zbior(class Zbior const &)" (??0?$Zbior@H@@QAE@ABV0@@Z) referenced in function "public: class Zbior __thiscall Zbior::operator+(class Zbior const &)" (??H?$Zbior@H@@QAE?AV0@ABV0@@Z)

This is telling you that you have declared:

template<class T>
class Zbior
{
public:
  [...]
    Zbior<T>(Zbior<T> const &other);    

but not defined it.

And in fact when I look through your other posted code I find this to be true. There is no definition for this copy constructor.

John Dibling
  • 99,718
  • 31
  • 186
  • 324