-5

I have this code where I am trying to use the same template in two different class. When I compile, I get error:

#include <iostream>
#include <memory>

template <class T>
class Node

{
    public:
    int value;
    std::shared_ptr<Node> leftPtr;
    std::shared_ptr<Node> rightPtr;
    Node(int val) : value(val) {
         std::cout<<"Contructor"<<std::endl;
    }
    ~Node() {
         std::cout<<"Destructor"<<std::endl;
    }
};

template <class T>
class BST {

    private:
        std::shared_ptr<Node<T>> root;

    public:
        void set_value(T val){

            root->value = val;
        }
        void print_value(){
            std::cout << "Value: " << root.value << "\n";
        }
};

int main(){

    class BST t;
    t.set_value(10);
    t.print_value();

    return 1;
}

Errors:

g++ -o p binary_tree_shared_pointers.cpp --std=c++14
binary_tree_shared_pointers.cpp:39:8: error: elaborated type refers to a template
        class BST t;
              ^
binary_tree_shared_pointers.cpp:21:7: note: declared here
class BST {
      ^
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Puneet Goswami
  • 61
  • 3
  • 10
  • `root.value` or `root->value`? – Some programmer dude Jul 14 '17 at 06:18
  • `class BST t;` Are you coming from C? 1) That's not legal C++ and on an unrelated note you also don't need `struct`, but that it allowed 2) You need to specify the template parameter – Rakete1111 Jul 14 '17 at 06:19
  • As for the error you ask about, you *do* know how to use templated classes? You do it with the `Node` template? Perhaps you should take a few steps back, and [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Jul 14 '17 at 06:19
  • class BST t; is it wrong? But that is creating an object right? So whats wrong in it? What is a template parameter? Everywhere I saw template, I saw that only. – Puneet Goswami Jul 14 '17 at 06:22
  • Just another thing - make sure to enable compiler warnings. This way you will get (a) more explicit messages (b) way more messages. For g++ you could do this: `g++ --std=c++14 -Wall -Wextra - Werror -pedantic -o p binary_tree_shared_pointers.cpp`. `Wall` enables "all" messages, `Wextra` enables messages not covered by `Wall` and `Werror` treats warnings as erros (totally optional but a great way to prevent error prone programming, especially when you have just started to learn programming) – muXXmit2X Jul 14 '17 at 06:35

1 Answers1

5

You have not specified the type of BST. A template is an incomplete type unless you specify it accordingly. Another option is that the compiler can deduce the type somehow. Otherwise it is an incomplete type - thus you got an error.

If you want to make a tree of type int for example it should be:

BST<int> t;
t.set_value(10);
t.print_value();

Note that you don't need the class keyword to declare t.

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • The thing is that I cannot define type as template are used for generic data type right? So if I am going to end up using BST for an integer tree then whats the use of template? – Puneet Goswami Jul 14 '17 at 06:44
  • 1
    A template is a blue print of a class. You can use it to write a lot of different classes with the same behavior. `BST intTree` is a tree containing only integers, `BST stringTree` would contain strings. However `intTree` can't hold strings and neither can `stringTree` hold integers. That's simply not the purpose of a template. – muXXmit2X Jul 14 '17 at 06:48
  • Cool. Makes sense :) Thank you..:) – Puneet Goswami Jul 14 '17 at 06:49
  • One last question. Since I have "std::shared_ptr> root;" in BST class. I would need to allocate memory for this too right in order to call set function. Otherwise it would crash? What is the equivalent of new in shared_ptr? if new is the one we will be using then how do we allocate memory for shared_ptr? – Puneet Goswami Jul 14 '17 at 06:50
  • `root = std::make_shared(Node())`. `std::make_shared` does the heap allocation for you. If you want to initialize a shared pointer with it's constructor you would need to do something like this `std::shared_pointer root (new Node())`. I would recommend reading a book about this topic. SO has a great list of books for beginners to advanced programmers ;-) – muXXmit2X Jul 14 '17 at 07:00