c++ - 创建指向对象的指针队列时出错

标签 c++

我正在尝试创建一个通用树。当我在 create_tree_by_depth() 函数中声明一个 node* 指针队列时,我收到错误“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;
}

最佳答案

您的代码在第 38 行的最开头有一个嵌入式控制字符(准确地说是“\20”):

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

此外,std::queue 没有名为first 的成员,您的意思可能是front

关于c++ - 创建指向对象的指针队列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606932/

相关文章:

c++ - 如何使用 Visual Studio 2010 进行输入重定向?

c++ - 带有 lambda [&] 的线程导致神秘的结果

C++ 不需要 5/9 并且似乎对它进行了类型转换

c++ - 为什么 std::stack 被认为是无用的?

c++ - 奇怪的段错误

c++ - 使用 SCons 构建外部代码树

c++ - 对符号 'exp@@GLIBC_2.2.5' 的 undefined reference

c++ - "undefined reference to ' QScriptEngine::QScriptEngine()"

c++ - 为什么编译器需要一个中间交换函数来推断类型?

C++ :- Replacing a piece of string by another string