我正在尝试创建一个通用树。当我在create_tree_by_depth()函数中声明一个node * pointers的队列时,我得到一个错误" Excpected Expression"。 队列仍未声明。 请随时指出任何其他错误或建议。
#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;
}
答案 0 :(得分:1)
您的代码在第38行的开头有一个嵌入的控制字符(确切地说是'\ 20'):
prog.cpp:38:1:错误:在程序中迷路'\ 20' 队列&LT;节点*&gt; Q; //错误:预期表达
^
此外,std::queue
没有名为first
的成员,您可能意味着front