缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int---->解决方法

写了一个对应让其出的错误(其实也挺不容易的喔 )

错误如下:

1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(9): error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(9): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(9): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(10): error C2061: 语法错误: 标识符“Test1”
1>d:\work\win32project\testeachotherclude\testeachotherclude\testeachotherclude.cpp(15): error C2660: “Test2::className”: 函数不接受 1 个参数
1>d:\work\win32project\testeachotherclude\testeachotherclude\testeachotherclude.cpp(16): error C2039: “test1”: 不是“Test2”的成员
1>          d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(7) : 参见“Test2”的声明
1>d:\work\win32project\testeachotherclude\testeachotherclude\testeachotherclude.cpp(16): error C2227: “->refMe”的左边必须指向类/结构/联合/泛型类型

上传Test方便大家看:

main.cpp

#include "stdafx.h"
#include "Test1.h"
#include "Test2.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Test1* t1=new Test1();
	Test2* t2=new Test2();
	t1->className(t2);
	t1->test2->refMe();

	t2->className(t1);
	t2->test1->refMe();
	return 0;
}

再来看Test1的

Test1.h

#ifndef Test1_H
#define Test1_H

#include "Test2.h"

class Test1
{
public:
	Test2* test2;
	void Test1::className(Test2* test2);
	void refMe();
};
#endif;

Test1.cpp

#include "Test2.h"

void Test1::className(Test2* test2)
{
	this->test2=test2;
	std::cout<<"Test1  ";
}

void Test1::refMe()
{
	std::cout<<"refMe Test1"<

Test2.h

#ifndef Test2_H
#define Test2_H

#include "Test1.h"

class Test2
{
public:
	Test1* test1;
	void className(Test1* test1);
	void refMe();
};
#endif

Test2.cpp

#include "StdAfx.h"
#include "Test2.h"
#include 

void Test2::className(Test1* test1)
{
	this->test1=test1;
	std::cout<<"Test2  ";
}

void Test2::refMe()
{
	std::cout<<"refMe Test2"<


然后运行就报上面的一对错误了...

仔细看看发现两个头文件都彼此包含了,就是这个问题啦...那我们怎么解决了,上传我的代码,直接看这样更容易理解  main.cpp不变

Test1.h

#ifndef Test1_H
#define Test1_H
//#include "Test2.h" //用下面的方式替代
class Test2;//注意:只是告诉编译器,需要这个类,其他功能结构等都没

class Test1
{
public:
	Test2* test2;
	void Test1::className(Test2* test2);
	void refMe();
};
#endif;


 Test1.cpp

#include "StdAfx.h"
#include "Test1.h"
#include 

#include "Test2.h" //注意:这里才是真正的包含

void Test1::className(Test2* test2)
{
	this->test2=test2;
	std::cout<<"Test1  ";
}

void Test1::refMe()
{
	std::cout<<"refMe Test1"<


Tes2的我就不贴了,一样的道理,编译运行ok ....


你可能感兴趣的:(C++基础积累)