[英]class pointer error: does not have a type
我的類變量之一(它是指向另一個類的指針)給出了沒有類型的錯誤。 經過幾天的思考就能弄清楚,有人看到錯誤了嗎? 也出於某種原因
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:
};
任何幫助都倍受贊賞。
我的類變量之一(它是指向另一個類的指針)給出了沒有類型的錯誤。
嘗試使用gridnode
,尚未定義它。 例如,如果您在.h
文件中有循環引用,則可能會發生這種情況。 由於您只是聲明一個指針,因此可以使用前向聲明:
#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
也出於某種原因
r->setright(NULL)
使程序崩潰
在gridnode
構造函數內部,您正在調用l
, r
, u
和d
指針上尚未初始化為指向任何東西的方法。 您正在通過調用未定義的行為來調用它們上的方法。 您可能打算將指針本身設置為NULL
,例如:
gridnode(){
l = NULL;
r = NULL;
u = NULL;
d = NULL;
}
要么:
gridnode() :
l(NULL),
r(NULL),
u(NULL),
d(NULL)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.