By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

Problems templating a class for the first time

P: 5
I'm trying to template a class that's not cooperating with me. I followed all the instructions that my instructor and the book gave me, but it won't work.

I'm getting a total of 57 errors, most of them recurring once for each function in my implementation. One error in particular is
"C2988: Unrecognizable template declaration/definition"


I will include my header, and an example of how my functions are set up in the implementation.

Expand|Select|Wrap|Line Numbers
  1. #include <list>
  2.  
  3. #ifndef TRINODE_H
  4. #define TRINODE_H
  5.  
  6. template <class myType>
  7. class trinode
  8. {
  9. public:
  10.  
  11.     //CONSTRUCTOR//
  12.     trinode(myType init_data =0);
  13.     //Creates a trinode containing specified data_field or default 0
  14.  
  15.     //MEMBER FUNCTIONS//
  16.  
  17.     void set_data(myType new_data);
  18.     //sets the data_field in the root
  19.     void insert_node(const myType&entry);
  20.     //Member Function to insert a new node in any direction
  21.     void remove_head(std::string direction);
  22.     //Member functions to remove a node from the begin of list
  23.     void remove_tail(std::string direction);
  24.     //Member functions to remove a node from the end of list
  25.  
  26.  
  27.     //CONSTANT MEMBER FUNCTIONS//
  28.  
  29.     int data() const;
  30.     //const member functions to retrieve current data:
  31.  
  32.     //Member functions to retrieve links:
  33.     const std::list<myType>* left() const;
  34.     std::list<myType>* left();
  35.     const std::list<myType>* right() const;
  36.     std::list<myType>* right();
  37.     const std::list<myType>* down() const;
  38.     std::list<myType>* down();
  39.  
  40.  
  41. private:
  42.     myType data_field;
  43.     std::list<myType>* left_link;
  44.     std::list<myType>* right_link;
  45.     std::list<myType>* down_link; 
  46. };
  47.  
  48. #include "trinode.cpp"
  49. #endif
  50.  

EXAMPLE OF IMPLEMENTATION OF A CTOR AND FUNCTION
Expand|Select|Wrap|Line Numbers
  1. //CONSTRUCTOR: Creates a trinode containing specified initial values
  2. template <class myType>
  3. trinode<myType>::trinode(myType init_data)
  4. {
  5.     data_field = init_data;
  6.  
  7.     left_link = new std::list<myType>;
  8.     right_link = new std::list<myType>;
  9.     down_link = new std::list<myType>;
  10. }
  11.  
  12.  
  13. //Member functions to set the data
  14. template <class myType>
  15. void trinode<myType>::set_data(myType new_data)
  16. {
  17.     data_field = new_data;
  18. }
  19.  
Does anyone see any obvious problems? I tried adding typename in with myType and that didn't make a difference, same errors.
Oct 26 '10 #1
Share this Question
Share on Google+
2 Replies


P: 5
and why would it say:

"error C2039: 'insert_node' : is not a member of '`global namespace"

on a lot of my functions, of course 'insert_node' is replaced with the other function names
Oct 26 '10 #2

100+
P: 687
Though including .cpp from .h file is a bit strange, it works for me - I wrote main.cpp to include trinode.h and
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   trinode<int> t;
  4.   t.insert_node(1);
  5. }
didn't link obviously because of lack of insert() but it compiled. Of course if you try to compile trinode.cpp it fails because it contains no declarations of used types.
Oct 27 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.