我的一个类变量是指向另一个类的指针,它给出了一个没有类型的错误。经过几天的思考可以弄清楚,有人看到了错误吗?也出于某种原因行
r->setright(NULL)
崩溃程序
#include "gridnode.h"
#include "grid.h"
#include "basenode.h"
class grid
{
public:
grid();
void print();
protected:
gridnode *head;//error of no type here
};
#endif // GRID_H
和我的节点类
class gridnode:public basenode
{
public:
char mark;
gridnode *l, *r, *u, *d;
gridnode(){
l->setleft(
r->setright(NULL);//crashes
u->setup(NULL);
d->setdown(NULL);
}
//set and get functions
protected:
};
任何帮助都有很大帮助。
答案 0 :(得分:1)
one of my class variables that is a pointer to another class is giving a error of no type.
gridnode
has not been defined yet when you try to use it. That can happen if you have circular references in your .h
files, for instance. Since you are just declaring a pointer, you can use a forward declaration instead:
#ifndef GRID_H
#define GRID_H
// there should be no include of grid.h here
// move the gridnode.h and basenode.h includes to grid.cpp instead
class gridnode; // forward declaration
class grid
{
public:
grid();
void print();
protected:
gridnode *head;
};
#endif // GRID_H
also for some reason the line
r->setright(NULL)
crashes the program
Inside the gridnode
constructor, you are calling methods on l
, r
, u
, and d
pointers that have not been initialized to point at anything. You are invoking undefined behavior by calling methods on them. You probably meant to set the pointers themselves to NULL
instead, eg:
gridnode(){
l = NULL;
r = NULL;
u = NULL;
d = NULL;
}
Or:
gridnode() :
l(NULL),
r(NULL),
u(NULL),
d(NULL)
{
}