vendredi 20 décembre 2019

undefined reference to class - c++ [duplicate]

This question already has an answer here:

Keep getting this error, but don't understand why. I figure my syntax is off in the subclass constructor, but can't find out the problem specifically:

g++ -o p7 main.o Pathfinder.o WNode.o Node.o
WNode.o: In function `WNode::WNode(word*, std::map<word, std::unordered_set<Node<word>*, std::hash<Node<word>*>, std::equal_to<Node<word>*>, std::allocator<Node<word>*> >*, std::less<word>, std::allocator<std::pair<word const, std::unordered_set<Node<word>*, std::hash<Node<word>*>, std::equal_to<Node<word>*>, std::allocator<Node<word>*> >*> > >*)':
WNode.cpp:(.text+0x43): undefined reference to `Node<word>::Node(word*, Node<word>*, Node<word>*)'
WNode.cpp:(.text+0x335): undefined reference to `Node<word>::~Node()'
collect2: error: ld returned 1 exit status
make: *** [p7] Error 1

The superclass has a protected constructor, destructor

With a default value for the first parameter, the program still gives this error; ie T* = new T()

//... [superclass.h]
template <typename T> class Pathfinder;
template <typename T> class Node{
 public: friend class Pathfinder<T>;
 protected:
   Node(T*, Node<T>* = 0, Node<T>* = 0);
  ~Node();
  T* value;
  Node<T>* prev;
  // inline overloaded operators ==,<,<=,>,>= and <<(ostream& out,const Node<T>& obj)
  // members: fCost(), gCost, hCost
  virtual double distance(T*,T*) const = 0; //... distance(Node<T>*)... distance(Node<T>*,Node<T>*)
  void setPrev(Node<T>*); 
  void setLast(Node<T>*);
  virtual Node<T>** getNextNodes() const = 0;
};

//... [superclass.cpp]
template <typename T>
Node<T>::Node(T* value,Node<T>* prev,Node<T>* last){
  setPrev(prev); //calculate and update gCost and update prev
  setLast(last); //calculate and update hCost
}
template <typename T>
Node<T>::~Node(){
  if(value){ delete value; value = 0; } 
  if(prev){ prev = 0; }
}
//...

The subclass has a private constructor, destructor

//... [subclass.h]
typedef word derivative;
typedef map<derivative,unordered_set<Node<word>*>*> NodeConnections;
class WNode : Node<word>{ 
 public: friend class Pathfinder<word>;
 private:
  WNode(word*, NodeConnections*);
  inline ~WNode(){ connections = 0; }
  virtual double distance(word*,word*) const;
  virtual Node<word>** getNextNodes() const;
  NodeConnections* connections; //could be static: necessary to find next nodes
  derivative** getDerivatives(word) const;
};

//... [subclass.cpp]
typedef word derivative;
typedef unordered_set<Node<word>*> AdjacentWords;
typedef map<derivative,AdjacentWords*> NodeConnections;
WNode::WNode(word* _word,NodeConnections* _connections): Node<word>(_word,NULL,NULL){
  //do stuff
}
//...

Help.

Aucun commentaire:

Enregistrer un commentaire