Thread: Template question

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    Template question

    So i'm trying to convert a postfix expression to an expression tree and print it. I have the following classes. In the main, I have never really understood how to properly declare stuff. Im getting errors saying the function i used in main has not been declared and such. Any explanation would be appreciated

    Code:
    template<class object>
    class TreeNode
    {
      object element;
      TreeNode *left;
      TreeNode *right;
      TreeNode(const object &theElement, TreeNode *lt, TreeNode *rt):
        element(theElement), left(lt), right(rt){}
    };
    template<class object>
    class Stack
    {
    public:
      Stack();
      ~Stack();
      bool isEmpty();
      TreeNode<object> *top();
      void makeEmpty();
      void pop();
      void push(TreeNode<object> *info);
      TreeNode<object> *topAndPop();
      void posttotree(string, TreeNode<object>*&);
    private:
      struct ListNode {
        TreeNode<object> * element;
        ListNode *next;
        ListNode(const object &theElement, ListNode *n=NULL):
          element(theElement), next(n) {}
      };
      ListNode *topofstack;
    };
    
    int main()
    {
      string postfix;
      TreeNode<object> *T;  // not properly declared
      cout << "Enter postfix expression:" << endl;
      cin >> postfix;
      posttotree(postfix, *T);   // function not declared?
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you need to create a stack object in order to use the posttotree() function. it's a member function and cannot be called independently of an object. also, line 36 is a problem because there is no such thing as an 'object' type in the context of your program. you need to use a real type like int or string in place of object in main.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Is using a template better? I keep getting errors such as "Stack is not a template" or "TreeNode<char>* TreeNode<char>::left is private within this context"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by stefanyco
    Is using a template better?
    Better than what? Elkvis pointed out that using the name object in the main function is wrong because that name does not exist within that scope.
    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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Better than no template, in this context.
    And yes, got that.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problems have nothing to do with templates and the fact that you seem confused on basics.
    If you've used std::vector, then you know it wants a type. So does your tree. But you are telling it to use a non-existans type. The type "object" does not exist.
    Furthermore, you are creating a pointer to a that tree, for absolutely no reason. Why?
    posttotree is a member function in the Stack, yet you try to use it as a global function, and passing the wrong number of arguments, as well. Why?

    Perhaps you should review basics.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Oh no no, I understand that. I knew that I was just frustrated when I posted this and posted prematurely. I'm getting different errors now such as 'Stack is not a template' or 'template argument required for struct Stack'
    I was thinking of just making it a non template to just see if my algorithms work. But why would I get the error "Stack is not a template" for example, when it says template. haha I'll figure it out in due time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template question
    By wxjeacen in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2009, 02:18 AM
  2. Template question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 10-12-2008, 07:16 AM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 03:52 PM
  4. Template question
    By grscot in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2003, 03:12 PM