|
我的程序里有如下的代码:
[code:1]
//*****id.h*****/
//#include "list.h" <--------注意这行
class Id{
public:
Id();
~Id();
public:
//static List* list; <--------注意这行
int id;
};
[/code:1]
[code:1]
//*****id.cpp*****/
#include "id.h"
//List* Id::list=NULL; <--------注意这行
Id::Id()
{
id=-1;
// list->add(this); <--------注意这行
}
Id::~Id()
{
}
[/code:1]
[code:1]
//*****list.h*****/
#include <stddef.h>
#include "id.h" <--------注意这行
#include "list_node.h"
class List{
public:
List();
~List();
Id* find(int _id);
void add(Id* _obj);
void del(int _id);
// void clear(int _id);
protected:
List_node objs[10];
List_node* bobjs[10];
int rw2;
int rw1;
};
[/code:1]
[code:1]
//*****list.cpp*****/
#include "list.h"
List::List()
{
for( int i=0;i<10;++i ){
objs[i].id=i;
bobjs[i]=NULL;
}
rw1=0;
rw2=-1;
}
List::~List()
{
}
/*!
\fn List::find(int _id)
*/
Id* List::find(int _id)
{
if (objs[_id].obj!=NULL){
return objs[_id].obj;
}
else{
return NULL;
}
}
/*!
\fn List::add(Id* _obj)
*/
void List::add(Id* _obj)
{
if ( rw2>=0 ){
bobjs[rw2]->obj=_obj;
bobjs[rw2]->obj->id=bobjs[rw2]->id;
--rw2;
}
else{
if ( rw1<=10-1 ){
objs[rw1].obj=_obj;
objs[rw1].obj->id=objs[rw1].id;
++rw1;
}
else{
//new page
}
}
}
/*!
\fn List::del(int _id)
*/
void List::del(int _id)
{
if ( objs[_id].obj!=NULL ){
delete objs[_id].obj;
objs[_id].obj=NULL;
bobjs[++rw2]=&objs[_id];
}
}
[/code:1]
出错信息如下:
……
In file included from /home/sjinny/Prj/idtest/src/id.h:23,
*from /home/sjinny/Prj/idtest/src/id.cpp:20:
*/home/sjinny/Prj/idtest/src/list.h:37: error: syntax error before `*' token
*/home/sjinny/Prj/idtest/src/list.h:38: error: `Id' was not declared in this scope
*/home/sjinny/Prj/idtest/src/list.h:38: error: `_obj' was not declared in this scope
*/home/sjinny/Prj/idtest/src/list.h:38: error: invalid data member initialization
*/home/sjinny/Prj/idtest/src/list.h:38: error: (use `=' to initialize static data members)
*/home/sjinny/Prj/idtest/src/list.h:38: error: variable or field `add' declared void
……
In file included from /home/sjinny/Prj/idtest/src/id.h:23,
*from /home/sjinny/Prj/idtest/src/obj1.h:23,
*from /home/sjinny/Prj/idtest/src/obj1.cpp:20:
*/home/sjinny/Prj/idtest/src/list.h:37: error: syntax error before `*' token
*/home/sjinny/Prj/idtest/src/list.h:38: error: `Id' was not declared in this scope
*/home/sjinny/Prj/idtest/src/list.h:38: error: `_obj' was not declared in this scope
*/home/sjinny/Prj/idtest/src/list.h:38: error: invalid data member initialization
*/home/sjinny/Prj/idtest/src/list.h:38: error: (use `=' to initialize static data members)
*/home/sjinny/Prj/idtest/src/list.h:38: error: variable or field `add' declared void
……
In file included from /home/sjinny/Prj/idtest/src/id.h:23,
*from /home/sjinny/Prj/idtest/src/obj1.h:23,
*from /home/sjinny/Prj/idtest/src/obj2.h:23,
*from /home/sjinny/Prj/idtest/src/obj2.cpp:20:
*/home/sjinny/Prj/idtest/src/list.h:37: error: syntax error before `*' token
*/home/sjinny/Prj/idtest/src/list.h:38: error: `Id' was not declared in this scope
*/home/sjinny/Prj/idtest/src/list.h:38: error: `_obj' was not declared in this scope
*/home/sjinny/Prj/idtest/src/list.h:38: error: invalid data member initialization
*/home/sjinny/Prj/idtest/src/list.h:38: error: (use `=' to initialize static data members)
*/home/sjinny/Prj/idtest/src/list.h:38: error: variable or field `add' declared void
……
在id.h中,以及id.cpp中,有几行被注释掉了,如果取消这几个注释,那么编译时就会抱错,反复实验,发现头文件似乎不能被互相引用……即:
[code:1]
对于两个头文件:foo1.h和foo2.h
它们不能互相include
比如foo1.h中#include "foo2.h",同时foo2.h中又#include "foo1.h",编译时就会抱错。
[/code:1]
我试了一下,出错信息如下:
[sjinny@localhost t2]$ make
g++ -o foo foo.cpp
In file included from foo2.h:1,
from foo1.h:1,
from foo2.h:1,
from foo1.h:1,
from foo2.h:1,
from foo1.h:1,
from foo2.h:1,
from foo1.h:1,
from foo2.h:1,
from foo1.h:1,
from foo2.h:1,
同样的信息还有很多行……
from foo1.h:1,
from foo.cpp:1:
foo1.h:1:10: #include nested too deeply
make: *** [foo] 错误 1
不知道有什么好办法…… |
|