template< class K, class V > class Map;
template< class K, class V > class MapIter;
template< class K, class V > class Link
{
friend class Map< K, V >;
friend class MapIter< K, V >;
private:
const K key_;
V value_;
Link *pre_, *suc_;
Link(const K& k, const V& v ): key_(k), value_(v), pre_(0), suc_(0){}
~Link()
{
delete suc_;
}
};
template< class K, class V > class Map
{
friend class MapIter< K , V >;
Link< K, V >* head_;
K def_key_;
V def_value_;
size_t size_;
static K kdef();
static V vdef();
void init()
{
size_=0;
head_=0;
}
public:
Map();
Map( const K&, const V& );
Map(const Map&);
~Map();
MapIter< K, V > find(const K& );
Map& operator=(const Map&);
V& operator[](const K&);
V& operator[](int);
int size() const;
void clear();
void add(const K&, const V&);
void add(const K* , const V* , int);
void remove(const K&);
MapIter< K, V > first();
MapIter< K, V > last();
};
And a cpp file that contains the definitions for it's members.This is just a fragment because the whole source is just too big to show.
#include "Map.h"
template< class K , class V >
Map< K , V >::Map()
{
this->init();
this->def_key_ = Map< K , V >::kdef();
this->def_value_ = Map< K , V >::vdef();
}
template< class K , class V >
Map< K , V >::Map(const K& key, const V& value)
{
this->init();
this->def_key_ = key;
this->def_value_ = value;
}
template< class K , class V >
Map< K , V >::Map(const Map< K , V >& mp)
{
if(mp.size_ == 0)
{
this->init();
return;
}
this->size_ = mp.size_;
this->head_ = new Link< K , V >(mp.head_->key,mp.head_->value_);
Link< K , V > *pos = this->head_;
Link< K , V > *pos2 = mp.head_->suc_;
for(int i = 1 ;i < this->size_; i++)
{
Link< K , V > *aux = new Link< K , V >(pos2->key_,mp.pos2->value_);
pos->suc_ = aux;
aux->pre_ = pos;
pos = pos->suc_;
}
}
template< class K , class V >
Map< K , V >::~Map()
{
delete this->head_;
}
I don't understand why when I try to compile a program using these sources always gives errors,unresolved external symbol ..., at compile for each method/contructor/destructor that if defined in the header. Basically the compiler can't find/use all those methods definitions. I'm pretty new to templates so I'm guessing that I missed something important.
Question
Cold Blood
Ok so I have a header that defines this class
template< class K, class V > class Map; template< class K, class V > class MapIter; template< class K, class V > class Link { friend class Map< K, V >; friend class MapIter< K, V >; private: const K key_; V value_; Link *pre_, *suc_; Link(const K& k, const V& v ): key_(k), value_(v), pre_(0), suc_(0){} ~Link() { delete suc_; } }; template< class K, class V > class Map { friend class MapIter< K , V >; Link< K, V >* head_; K def_key_; V def_value_; size_t size_; static K kdef(); static V vdef(); void init() { size_=0; head_=0; } public: Map(); Map( const K&, const V& ); Map(const Map&); ~Map(); MapIter< K, V > find(const K& ); Map& operator=(const Map&); V& operator[](const K&); V& operator[](int); int size() const; void clear(); void add(const K&, const V&); void add(const K* , const V* , int); void remove(const K&); MapIter< K, V > first(); MapIter< K, V > last(); };And a cpp file that contains the definitions for it's members.This is just a fragment because the whole source is just too big to show.
#include "Map.h" template< class K , class V > Map< K , V >::Map() { this->init(); this->def_key_ = Map< K , V >::kdef(); this->def_value_ = Map< K , V >::vdef(); } template< class K , class V > Map< K , V >::Map(const K& key, const V& value) { this->init(); this->def_key_ = key; this->def_value_ = value; } template< class K , class V > Map< K , V >::Map(const Map< K , V >& mp) { if(mp.size_ == 0) { this->init(); return; } this->size_ = mp.size_; this->head_ = new Link< K , V >(mp.head_->key,mp.head_->value_); Link< K , V > *pos = this->head_; Link< K , V > *pos2 = mp.head_->suc_; for(int i = 1 ;i < this->size_; i++) { Link< K , V > *aux = new Link< K , V >(pos2->key_,mp.pos2->value_); pos->suc_ = aux; aux->pre_ = pos; pos = pos->suc_; } } template< class K , class V > Map< K , V >::~Map() { delete this->head_; }I don't understand why when I try to compile a program using these sources always gives errors,unresolved external symbol ..., at compile for each method/contructor/destructor that if defined in the header. Basically the compiler can't find/use all those methods definitions. I'm pretty new to templates so I'm guessing that I missed something important.
Link to comment
Share on other sites
5 answers to this question
Recommended Posts