Templates and classes

This is a discussion on Templates and classes within the C++ Programming forums, part of the General Programming Boards category; I keep getting bizzare errors when trying to compile the following code..These are standard template classes. I have the following ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    41

    Templates and classes

    I keep getting bizzare errors when trying to compile the following code..These are standard template classes.


    I have the following two files:

    BraidedNode.cpp
    Code:
    #include "BraidedNode.h"
    
    template<class T> BraidedNode<T>::BraidedNode(T val) :
      TreeNode<T>(val)
    {
    }
    
    template<class T> *BraidedNode<T>::rchild(void) :
      TreeNode<T>(val)
    {
      return (BraidedNode<T>*)_rchild;
    }
    
    template<class T> *BraidedNode<T>::lchild(void) :
      TreeNode<T>(val)
    {
      return (BraidedNode<T>*)_lchild;
    }
    
    
    template<class T> *BraidedNode<T>::next(void) :
      TreeNode<T>(val)
    {
      return (BraidedNode<T>*)_next;
    }
    
    template<class T> *BraidedNode<T>::prev(void) :
      TreeNode<T>(val)
    {
      return (BraidedNode<T>*)_prev;
    }
    BraidedNode.h

    Code:
    #ifndef _BraidedNode_h
    #define _BraidedNode_h
    
    #include <stdio.h>
    #include "Node.h"
    #include "TreeNode.h"
    
    template<class T>class BraidedNode : public Node, public TreeNode<T>
    {
      public:
      BraidedNode(T);
      BraidedNode<T> *rchild(void);
      BraidedNode<T> *lchild(void);
      BraidedNode<T> *next(void);
      BraidedNode<T> *prev(void);
      //friend class BraidedSearchTree<T>;
    };

    and the error I get is:

    Code:
    BraidedNode.cpp:8: error: ISO C++ forbids declaration of ‘rchild’ with no type
    BraidedNode.cpp:8: error: prototype for ‘int* BraidedNode<T>::rchild()’ does not match any in class ‘BraidedNode<T>’
    BraidedNode.h:12: error: candidate is: BraidedNode<T>* BraidedNode<T>::rchild()
    BraidedNode.cpp:8: error: template definition of non-template ‘int* BraidedNode<T>::rchild()’
    BraidedNode.cpp: In member function ‘int* BraidedNode<T>::rchild()’:

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Code:
    template<class T> BraidedNode<T> *BraidedNode<T>::rchild(void) :
      TreeNode<T>(val) // part in red is wrong 
    {
      return (BraidedNode<T>*)_rchild; // The cast is unnecessary
    }
    And same for the others.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Some advice: don't "copy and paste" bits... type them. You've copied over the constructor initialization list and forgot to add the return type.

    Soma

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    whoops, bit of a typo: the error I have now is:

    BraidedNode.cpp: In member function ‘BraidedNode<T>* BraidedNode<T>::rchild()’:
    BraidedNode.cpp:11: error: ‘_rchild’ was not declared in this scope
    BraidedNode.cpp: In member function ‘BraidedNode<T>* BraidedNode<T>::lchild()’:
    BraidedNode.cpp:17: error: ‘_lchild’ was not declared in this scope

    rchild should be defined....any ideas....

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    Code:
    #include "BraidedNode.h"
    
    
    template<class T> BraidedNode<T>::BraidedNode(T val) 
    : TreeNode<T>(val)
    {
    }
    
    template<class T> 
    BraidedNode<T> *BraidedNode<T>::rchild(void) 
    {
      return (BraidedNode<T>*)_rchild;
    }
    
    template<class T>
    BraidedNode<T> *BraidedNode<T>::lchild(void) 
    {
      return (BraidedNode<T>*)_lchild;
    }
    
    template<class T> 
    BraidedNode<T> *BraidedNode<T>::next(void) 
    {
      return (BraidedNode<T>*)_next;
    }
    
    template<class T> 
    BraidedNode<T> *BraidedNode<T>::prev(void) 
    {
      return (BraidedNode<T>*)_prev;
    }
    and

    Code:
    #ifndef _BraidedNode_h
    #define _BraidedNode_h
    
    #include <stdio.h>
    #include "Node.h"
    #include "TreeNode.h"
    
    template<class T>class BraidedNode : public Node, public TreeNode<T>
    {
      public:
      BraidedNode(T);
      BraidedNode<T> *rchild(void);
      BraidedNode<T> *lchild(void);
      BraidedNode<T> *next(void);
      BraidedNode<T> *prev(void);
      //friend class BraidedSearchTree<T>;
    };
    
    
    
    #endif

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Note that it's usually not a good idea to put templates into .cpp files. As to why, I don't have the link, but I'm sure someone else does.
    Templates go into header files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    So... you really don't have any data members? Or are they inherited?

    Soma

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Note that it's usually not a good idea to put templates into .cpp files. As to why, I don't have the link, but I'm sure someone else does.
    Templates go into header files.
    One such explanatory link would be: Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?

    That said, that is not (yet) the problem.

    So... you really don't have any data members?
    Inherited non-private data members, perhaps? Or maybe they are actually private, thus they are not in scope, as the error message says.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    Quote Originally Posted by phantomotap View Post
    So... you really don't have any data members? Or are they inherited?

    Soma
    Should be inheriting from the Node class and Tree Node class, which are as follows:

    Code:
    template<class T> class TreeNode {
    protected:
      TreeNode *_lchild;
      TreeNode *_rchild;
      T val;
    public:
      TreeNode(T);
      virtual ~TreeNode(void);
      //friend class SearchTree<T>;
      //friend class BraidedSearchTree<T>;
    };
    and as follows....

    Code:
    class Node {
    protected:
      Node *_next;
      Node *_prev;
    public:
      Node(void);
      virtual ~Node(void);
      Node *next(void);
      Node *prev(void);
      Node *insert(Node*);
      Node *remove(void);
      void splice(Node*);
    };
    Any ideas?

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Add 'this->' to every reference of an inherited data member.

    Add 'this->' to every reference of an inherited method (function).

    Add 'base_type::' to every reference of an inherited 'typedef'.

    Add 'base_type::' to every reference of an inherited nested class.

    Soma

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Add 'this->' to every reference of an inherited data member.
    Oh yes, good catch.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    Code:
    template<class T> class TreeNode {
    protected:
      TreeNode *_lchild;
      TreeNode *_rchild;
      T val;
    public:
      TreeNode(T);
      virtual ~TreeNode(void);
      //friend class SearchTree<T>;
      //friend class BraidedSearchTree<T>;
    };
    I have added the following base class, but now I get the messages below

    Code:
    template<class T> BraidedNode<T>::BraidedNode(T val) 
    : TreeNode<T>(
    
    val)
    {
    }
    
    template<class T> 
    BraidedNode<T> *BraidedNode<T>::rchild(void) 
    {
      return (BraidedNode<T>*)TreeNode::_rchild;
    }
    
    template<class T>
    BraidedNode<T> *BraidedNode<T>::lchild(void) 
    {
      return (BraidedNode<T>*)TreeNode::_lchild;
    }
    
    template<class T> 
    BraidedNode<T> *BraidedNode<T>::next(void) 
    {
      return (BraidedNode<T>*)_next;
    }
    
    template<class T> 
    BraidedNode<T> *BraidedNode<T>::prev(void) 
    {
      return (BraidedNode<T>*)_prev;
    }
    BraidedNode.cpp: In member function ‘BraidedNode<T>* BraidedNode<T>::rchild()’:
    BraidedNode.cpp:12: error: expected primary-expression before ‘*’ token
    BraidedNode.cpp:12: error: expected primary-expression before ‘)’ token
    BraidedNode.cpp:12: error: expected ‘;’ before ‘TreeNode’
    BraidedNode.cpp:12: error: ‘template<class T> class TreeNode’ used without template parameters

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Tell me, if you were walking along with a friend and he/she pointed to the sky, would you look at his finger? (Read ALL of what I wrote; don't assume things by skimming it.)

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating Templates or Classes in C
    By algatt in forum C Programming
    Replies: 1
    Last Post: 04-09-2007, 02:12 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. VC++6: exporting classes containing templates
    By ttt in forum Windows Programming
    Replies: 2
    Last Post: 09-15-2003, 11:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21