oracle3001
oracle3001

Reputation: 1100

Error with using defaults in copy constructor of a templated class

I have the following templated classes,

template <typename Real>
class Marker {

 typedef Wm5::Vector3<Real> Position ;
 typedef Wm5::Vector3<Real> Normal ;
 typedef Wm5::Vector3<Real> Color ;

 public:

  Marker(int id = -1, Position position = Wm5::Vector3<Real>::ZERO, Normal normal = Wm5::Vector3<Real>::ZERO, Color color = Wm5::Vector3<Real>::ZERO)
: id_(id), position_(position), normal_(normal), color_(color), cluster_(-1) {}

  ~Marker() {}

 private:

  int id_ ;
  Position position_ ;
  Normal normal_ ; 
  Color color_ ;
  int cluster_ ;

};


template <typename T> 
class MarkerSet {

    typedef Marker<T> MarkerT ;      
    typedef std::vector<MarkerT> MarkerVector ;

public:

    MarkerSet::MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) 
   {id_ = id; markers_ = markers;}      

   MarkerSet::~MarkerSet() {}

private:

    int id_ ;   
    MarkerVector markers_ ;

} ;

When I try to create a MarkerSet object by

MarkerSet<double> markerSet ; 

Getting this linker error,

error LNK2001: unresolved external symbol "public: __thiscall     MarkerSet<double>::MarkerSet<double>(int,class std::vector<class Marker<double>,class std::allocator<class Marker<double> > >)" (??0?$MarkerSet@N@@QAE@HV?$vector@V?$Marker@N@@V?$allocator@V?$Marker@N@@@std@@@std@@@Z)

I would be extremely grateful if somebody could give me a pointer in the right direction with regards to what I have done wrong.

Edit:

Ok I have narrowed it down to something rather odd.

in .h

  MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) 
{id_ = id; markers_ = markers;}    

builds fine

Rather than in .h

 MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) ;

in .cpp

 template <typename T>
 MarkerSet<T>::MarkerSet(int id, MarkerVector markers) {

  id_ = id ;
  markers_ = markers ;

}

Errors out in the way described above.

Any thoughts?

Upvotes: 0

Views: 152

Answers (2)

Sid
Sid

Reputation: 7631

Can you try using a different compiler? I tried playing with it and the following works fine for me with gcc. I eliminated the Wm5 members as I don't have those. Pasting the header and cpp:

test.h

#include <vector>

template <typename Real>
class Marker {

 public:

  Marker(int id = -1,int _position=1)
    : id_(id), position(_position){}

  ~Marker() {}

 private:

  int id_ ;
  int position ;
  Real r;

};


template <typename T> 
class MarkerSet {

    typedef Marker<T> MarkerT ;      
    typedef std::vector<MarkerT> MarkerVector ;

public:

    MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) 
      {id_ = id; markers_ = markers;std::cout<<"Called"<<std::endl;}      

   ~MarkerSet() {}

private:

    int id_ ;   
    MarkerVector markers_ ;

} ;

test.cpp

#include <iostream>
#include <vector>
#include "test.h"

using namespace std;


int main(int argc, const char **argv) {
  cout<<"Hello"<<endl;
  MarkerSet<double> ms;
  return -1;

}

cmd:

bash$ ./test
Hello
Called
bash$ 

Template class definition need to be in the header:

Why do C++ template definitions need to be in the header?

Upvotes: 1

gluk47
gluk47

Reputation: 1830

May it be so that you do not #include actual contructor body so that the linker does not generate required code for MarkerSet<double>::MarkerSet<double>(int,class std::vector<class Marker<double>,class std::allocator<class Marker<double> > >)"?

Upvotes: 0

Related Questions