c++ - Template class woes; Proper template signature? -
i implementing template class , not sure if have signatures correct. below i've included sample of code, showing couple of situations concerned about:
mymap.h:
#ifndef map_h #define map_h #include <iostream> #include <string> using namespace std; template <typename key, typename t> class map{ private: struct elem { }; public: map(); // constructs empty map map(const map &rhs); ~map(); map& operator=(const map &rhs); bool insert(key, t); t& operator[](key); }; template<typename key, typename t> ostream& operator<< (ostream&, const map<key, t>&); #include "mymap.cpp" #endif
and .cpp file:
#include mymap.h #include <iostream> #include <string> using namespace std; //assingment operator template < typename key , typename t > map< key , t >& map< key , t >::operator=(const map &rhs){ //avoid self assignment if( != &rhs ){ //snip } return *this; } //insert, return true if successful. template < typename key , typename t > bool map< key , t >::insert(key, t){ //snip }
as can see, , i'm sure aware, template signatures can pretty messy i've read. correct, or have blinding errors noticeable more practiced eye?
i know there million posts templates, i've read few guides them still cannot find on specific issue.
also, before inevitable "get rid of using namespace std" comments appear, include debugging , does end going away, promise:)
thanks!
edit: sorry confusion, seemed have missed important part of post... errors!
i have 5 of these, various line numbers:
mymap.cpp:41:1: error: ‘map’ not name type
and equal number of:
expected initializer before ‘<’ token
remove #include mymap.h
mymap.cpp
then
#include "mymap.h"
in test.cpp
and compile
g++ -g test.cpp
for template classes, has in header.
Comments
Post a Comment