Thread: Templates and classes

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    5,108
    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
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    5,108
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    5,108
    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
    28,413
    Add 'this->' to every reference of an inherited data member.
    Oh yes, good catch.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    5,108
    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