Prayansh Srivastava
Prayansh Srivastava

Reputation: 126

Error while creating a Queue of Pointers to an Object

I am trying to create a generic tree. When I declare a queue of node* pointers in create_tree_by_depth() function, I get an error "Excpected Expression". The queue remains undeclared. Please feel free to point out any other mistakes or suggestions.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
template <typename T>   class tree;

template <typename T>
class node
{
public:
T data;
vector<node*> child;
//queue<node*> Q2;   THIS DOES NOT SHOW ANY ERROR
node *parent;

node(): parent(NULL), data(0){}
node(T d): parent(NULL), data(d){}
template <typename temp> friend class tree;
};


template <typename T>
class tree
{
public:
node<T> *root;

tree(): root(NULL){}
void create_tree_by_branch();
void create_tree_by_depth();
};


template <typename T>
void tree<T>::create_tree_by_depth()       
{
T d=0;
int i=0;
queue< node<T>* > Q;   //ERROR: Expected Expression               

cout<<"Enter Root :";
cin>>d;
node<T> *temp = new node<T>(d);
root = temp;
Q.push(root);

while(Q.empty()==false)
{
    i=0;
    cout<<"Enter Data of Children: ";
    node<T> *curr_parent = Q.first();

    while(1)
    {
        cin>>d;
        if(d == -1)
        {   break;}

        node<T> *temp = new node<T>(d);
        curr_parent->child[i++] = temp;
        temp->parent = curr_parent;
    }

    Q.pop();
}
}


int main()
{
    tree<int> T;
    T.create_tree_by_depth();   //TRIED T.create_tree_by_depth<int>(); ALSO    
    return 0;
}

Upvotes: 0

Views: 93

Answers (1)

danielschemmel
danielschemmel

Reputation: 11116

Your code has an embedded control character at the very beginning of the line 38 (a '\20' to be exact):

prog.cpp:38:1: error: stray '\20' in program
queue< node* > Q; //ERROR: Expected Expression
^

Additionally, std::queue has no member called first, you probably mean front

Upvotes: 1

Related Questions