Lengdon
Lengdon

Reputation: 79

Why I am getting visual studio C++ errors while trying Template

The code compiles correctly. Then I get these Build errors when I try to run

Error C2955   'Node': use of class template requires template argument list.
Error C2514   'Node': class has no constructors

I do not know much about template so correct me wherever I am wrong. So the code below is my header file

#pragma once
#include<iostream>
using namespace std;
template<class T>
    class Node
    {
    public:
        Node *left, *right, *parent;
        T key;
        Node(){
            left = right = parent = NULL;
        }
    };
template<class T>
    class GenericBst {
        Node<T>* root;
        bool search(Node<T>* current, int value);//Finds a value is present
    public:
        GenericBst()
        {
            root = NULL;
        }
        void insertNode(int value);//Insertation of node
        bool search(int value);
    };

    template<class T>
    inline bool GenericBst<T>::search(Node<T>* current, int value)
    {
        if (current == NULL)
            return false;
        while (current != NULL)
        {
            if (current->key == value)
                return true;
            else
            {
                if (value < current->key)
                    current = current->left;
                else
                    current = current->right;
            }
        }
    }

    template<class T>
    inline void GenericBst<T>::insertNode(int value)
    {       
        Node<T>*temp = new Node();
        temp->key = value;

        Node<T> *current = root, *parent = NULL;

        while (current != NULL)
        {
            //Parent is assigned the current position and current goes to the child
            parent = current;
            if (value < current->key)
            {
                current = current->left;
            }
            else
            {
                current = current->right;
            }
        }
        //Now we check if parent of current is NULL then currnt node is at root
        if (parent == NULL)
        {
            root = temp;//temp was used to store the insertion value. Now root points to it.
        }
        else if (value < parent->key)
        {
            parent->left = temp;
        }
        else
        {
            parent->right = temp;

        }
        temp->parent = parent;
    }

    template<class T>
    inline bool GenericBst<T>::search(int value)
    {
        return search(root,value);
    }

The main class has

#include<iostream>
using namespace std;
#include "GenericBst.h"
#include <iostream>

int main()
{
    GenericBst<int> Obj;
    //GenericBst<string> Obj;
    std::cout << "Welcome to generic World\n";
    int i = 1;
    int value;
    while (i != 0)
    {
        cout << "\nSelect:\n\t1. Insert a node.\t2. Search a Node\n\t0. Quit" << endl;
        switch (i)
        {
        case 0:
            i = 0;
            cout << "\n\t\tBye\n";
            break;
        case 1:
            cout << "Enter value: ";
            cin >> value;
            Obj.insertNode(value);
            break;
        case 2:
            cout << "Enter value: ";
            cin >> value;
            cout << Obj.search(value);
            break;
        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

Konstantin T.
Konstantin T.

Reputation: 1013

You should explicitly specify template parameter in template class.

Node<T>*temp = new Node();

Must be:

Node<T>*temp = new Node<T>();

Upvotes: 2

Related Questions