gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:09:18 24 4
gpt4 key购买 nike

我正在尝试创建一个通用树。当我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com