2013-06-06 4 views
-5

Я пишу простой код на дереве квадрантов, но я получаю эту одну ошибкуОшибка при определении функции-члена класса

#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(); 
} 

error- quad_tree.cpp: 45: ошибка: ожидается, конструктор, деструктор, или преобразование типов до '*' токена

В чем проблема?

P.S: Я комментировал линии нет 45

+6

пожалуйста, вы можете отступ кода? ... –

+2

Попробуйте 'qtree :: узел *' ... –

+0

благодаря нефти ... получило это – Nagsaver

ответ

0

Ваш узел структуры определяется только внутри класса qtree.

Если вы хотите использовать его вне методов qtree, вы должны определить его вне его.

В противном случае вы можете использовать предложение Оли с

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; 
} 

Plus: вы забыли с помощью патезраса, так как вы используете соиЬ позже. Используйте это или замените его на std :: cout.

+0

благодаря rob013 выпрямляются й ошибка – Nagsaver

0
node* qtree :: buildtree(char *a,int *l1,int *l2,int *r1,int *r2,int index) //LINE 45 

Там нет определения node в области. Вы хотите qtree::node.

+0

Благодаря выпрямленному его – Nagsaver

Смежные вопросы