定义类的成员函数时出错
Error with defining member function of a class
我正在编写一个简单的四叉树代码,但我得到这个错误
#include<iostream>
class qtree
{
struct node
{
node *left1;node *left2;
char data;
node *right1;node *right2;
}*root;
char *arr;
int *lc1;int *lc2;int *rc1;int *rc2;
public:
qtree(char *a,int *l1,int *l2,int *r1,int *r2,int size);
void insert(int index);
static node* buildtree(char *a,int *l1,int *l2,int *r1,int *r2,int index);
void display();
static void inorder(node *sr);
~qtree();
static void del(node *sr);
};
qtree::qtree(char *a,int *l1,int *l2,int *r1,int *r2,int size)
{
root = NULL;
arr = new char[size];
lc1 = new int[size];lc2 = new int[size];rc1 = new int[size];rc2 = new int[size];
for(int i=0;i<size;i++)
{
*(arr+i)=*(a+i);
*(lc1+i)=*(l1+i);*(lc2+i)=*(l2+i);
*(rc1+i)=*(r1+i);*(rc2+i)=*(r2+i);
}
}
void qtree::insert(int index)
{
root = buildtree(arr,lc1,lc2,rc1,rc2,index);
}
node* qtree :: buildtree(char *a,int *l1,int *l2,int *r1,int *r2,int index) //LINE 45
{
node* temp =NULL;
if(index!=-1)
{
temp = new node;
temp->left1 = buildtree(a,l1,l2,r1,r2,*(l1+index));
temp->left2 = buildtree(a,l1,l2,r1,r2,*(l2+index));
temp->data = *(a+index);
temp->right1 = buildtree(a,l1,l2,r1,r2,*(r1+index));
temp->right2 = buildtree(a,l1,l2,r1,r2,*(r2+index));
}
return temp;
}
void qtree::display()
{
inorder(root);
}
void qtree::inorder(node *sr)
{
if(sr!=NULL)
{
inorder(sr->left1);
inorder(sr->left2);
cout<<sr->data<<"t";
inorder(sr->right1);
inorder(sr->right2);
}
}
qtree::~qtree()
{
delete arr;
delete lc1;
delete lc2;
delete rc1;
delete rc2;
del(root);
}
void qtree::del(node *sr)
{
if(sr!=NULL)
{
del(sr->left1);
del(sr->left2);
del(sr->right1);
del(sr->right2);
}
delete sr;
}
void main()
{
char a[] = {'A','B','C','D','E','F','G','H','I'};
int l1[] = {1,5,-1,-1,-1,-1,-1,-1,-1};
int l2[] = {2,6,-1,-1,-1,-1,-1,-1,-1};
int r1[] = {3,7,-1,-1,-1,-1,-1,-1,-1};
int r2[] = {4,8,-1,-1,-1,-1,-1,-1,-1};
int sz = sizeof(a);
qtree qt(a,l1,l2,r1,r2,sz);
qt.insert(0);
cout<<"nThe elements are"<<endl;
qt.display();
}
错误-quad_tree.cpp:45:错误:在"*"标记之前进行预期的构造函数、析构函数或类型转换
可能是什么问题??
附言:I已评论第 45 行
您的结构节点仅在 qtree 类中定义。
如果你想在 qtree 的方法之外使用它,你应该在它之外定义它。
否则,您可以将 oli 的建议与
qtree:node* qtree::buildtree()
试试这个:
#include<iostream>
using namespace std;
struct node {
node *left1;
node *left2;
char data;
node *right1;
node *right2;
};
class qtree {
struct node* root;
char *arr;
int *lc1;
int *lc2;
int *rc1;
int *rc2;
public:
qtree(char *a,int *l1,int *l2,int *r1,int *r2,int size);
void insert(int index);
static node* buildtree(char *a,int *l1,int *l2,int *r1,int *r2,int index);
void display();
static void inorder(node *sr);
~qtree();
static void del(node *sr);
};
qtree::qtree(char *a,int *l1,int *l2,int *r1,int *r2,int size) {
root = NULL;
arr = new char[size];
lc1 = new int[size];
lc2 = new int[size];
rc1 = new int[size];
rc2 = new int[size];
for(int i=0; i<size; i++) {
*(arr+i)=*(a+i);
*(lc1+i)=*(l1+i);
*(lc2+i)=*(l2+i);
*(rc1+i)=*(r1+i);
*(rc2+i)=*(r2+i);
}
}
void qtree::insert(int index) {
root = buildtree(arr,lc1,lc2,rc1,rc2,index);
}
node* qtree :: buildtree(char *a,int *l1,int *l2,int *r1,int *r2,int index) { //LINE 45
node* temp =NULL;
if(index!=-1) {
temp = new node;
temp->left1 = buildtree(a,l1,l2,r1,r2,*(l1+index));
temp->left2 = buildtree(a,l1,l2,r1,r2,*(l2+index));
temp->data = *(a+index);
temp->right1 = buildtree(a,l1,l2,r1,r2,*(r1+index));
temp->right2 = buildtree(a,l1,l2,r1,r2,*(r2+index));
}
return temp;
}
void qtree::display() {
inorder(root);
}
void qtree::inorder(node *sr) {
if(sr!=NULL) {
inorder(sr->left1);
inorder(sr->left2);
cout<<sr->data<<"t";
inorder(sr->right1);
inorder(sr->right2);
}
}
qtree::~qtree() {
delete arr;
delete lc1;
delete lc2;
delete rc1;
delete rc2;
del(root);
}
void qtree::del(node *sr) {
if(sr!=NULL) {
del(sr->left1);
del(sr->left2);
del(sr->right1);
del(sr->right2);
}
delete sr;
}
int main() {
char a[] = {'A','B','C','D','E','F','G','H','I'};
int l1[] = {1,5,-1,-1,-1,-1,-1,-1,-1};
int l2[] = {2,6,-1,-1,-1,-1,-1,-1,-1};
int r1[] = {3,7,-1,-1,-1,-1,-1,-1,-1};
int r2[] = {4,8,-1,-1,-1,-1,-1,-1,-1};
int sz = sizeof(a);
qtree qt(a,l1,l2,r1,r2,sz);
qt.insert(0);
cout<<"nThe elements are"<<endl;
qt.display();
return 0;
}
另外:您忘记了使用命名空间 std,因为您稍后使用 cout。使用它或将其替换为 std::cout。
node* qtree :: buildtree(char *a,int *l1,int *l2,int *r1,int *r2,int index) //LINE 45
没有范围内node
的定义。你想要qtree::node
.
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- PrintWindow函数在notepad.exe中出错
- 使用运算符调用 void 函数时出错<set>
- 为 NewObjectA() 函数创建 jvalues 的参数数组时出错 - JNI Invocation API
- 在定义的函数中使用常量时出错
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 使用根/C++时出错:没有匹配的构造函数来初始化'TTree'
- Arduino IDE 中使用 strncmp 函数时出错
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 使用一个参数的模板函数时出错(适用于 2)
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 使用类的构造函数时出错
- 在 c++ 的构造函数中分配对象向量时出错
- 为什么在函数参数前面添加 const 会出错?
- 调用析构函数时出错