请教:m.h
class M
{
public:
A aa;
B bb;
int m;
};a.h
class A
{
public:
int f1(){};
void f2(){};
};b.h
class B
{
public:
B()
int f1();
void f2();
};b.cpp
int B::f1()
{
m = aa.f1();
}
请教:
我想把所有的公共变量都定义在 类 M 里
问题1:
我在 int B::f1() 里使用了
m = aa.f1();
我需要把 在 类 B 里这样定义吗?class B
{
public:
A aa;
int f1();
void f2();
};还是怎么定义啊?如果是这样定义我在 B 类怎么构造 A 啊问题2:
还是用别的什么方法?谢谢
class M
{
public:
A aa;
B bb;
int m;
};a.h
class A
{
public:
int f1(){};
void f2(){};
};b.h
class B
{
public:
B()
int f1();
void f2();
};b.cpp
int B::f1()
{
m = aa.f1();
}
请教:
我想把所有的公共变量都定义在 类 M 里
问题1:
我在 int B::f1() 里使用了
m = aa.f1();
我需要把 在 类 B 里这样定义吗?class B
{
public:
A aa;
int f1();
void f2();
};还是怎么定义啊?如果是这样定义我在 B 类怎么构造 A 啊问题2:
还是用别的什么方法?谢谢
问题2:一般我个人喜欢在头文件中定义指针,减少依赖,如下
//b.h
class A;//在这里声明A
class B
{
private:
A* pa;
}//b.cpp#include "A.h"
void B::B()
{
pa = new A();
}
void B::~B()
{
if (pa != NULL)
{
delete pa;
pa = NULL;
}
}
LZ,就按你说的方法定义挺好的啊,有什么不妥的么,为什么一定要把变量都放M里....我是想把所有的公共变量放到一个类里啊?
这样好吗?如果这样做,那我上面的要怎么写啊?
举个例子,好吗?谢谢!
class A;//在这里声明A
class B
{
private:
A pa;
}//b.cpp#include "A.h"
void B::B()
{
// pa = new A();
}
void B::~B()
{
//if (pa != NULL)
//{
// delete pa;
// pa = NULL;
// }
}
请教:
如果我不用类指针
那么我到哪里构造 类 A 啊?
谢谢!
class B
{
private:
A* pa;
};--------------------Configuration: MyGame - Win32 Debug--------------------
Compiling...
GameSet.cpp
c:\xiaogw\h\gameset.h(89) : error C2512: 'A' : no appropriate default constructor available
c:\xiaogw\h\gameset.h(164) : warning C4150: deletion of pointer to incomplete type 'A'; no destructor called
c:\xiaogw\h\gameset.h(1) : see declaration of 'A'
WinMain.cpp
Error executing cl.exe.MyGame.exe - 1 error(s), 1 warning(s)
可是,如果我包含头文件:
#include "a.h"
class A;//在这里声明A
class B
{
private:
A* pa;
};则在
类 A 那里提示错误
这是什么原因?
谢谢!
我是这样写的:
a.hclass A
{
public:
int i;
A()
{
i = 0;
};b.hclass A;
class B
{
A* aa;
public:
B()
{
aa = new A;
}
~B()
{
if(aa != NULL)
{
delete aa;
aa = NULL;
}
}
};
我错在哪里啊?
我是这样写的:
a.hclass A
{
public:
int i;
A()
{
i = 0;
};b.hclass A;
class B
{
A* aa;
public:
B()
{
aa = new A;
}
~B()
{
if(aa != NULL)
{
delete aa;
aa = NULL;
}
}
};编译提示:
--------------------Configuration: MyGame - Win32 Debug--------------------
Compiling...
GameSet.cpp
c:\xiaogw\h\gameset.h(5) : error C2143: syntax error : missing ';' before '*'
c:\xiaogw\h\gameset.h(5) : error C2501: 'A' : missing storage-class or type specifiers
c:\xiaogw\h\gameset.h(5) : error C2501: 'aa' : missing storage-class or type specifiers
C:\XiaoGW\CPP\GameSet.cpp(142) : error C2065: 'aa' : undeclared identifier
Error executing cl.exe.MyGame.exe - 7 error(s), 0 warning(s)
我错在哪里啊?
a.hclass A
{
public:
int i;
A()
{
i = 0;
};b.hclass A;
class B
{
A* aa;
public:
B():aa (new A)
{
}
~B()
{
if(aa != NULL)
{
delete aa;
aa = NULL;
}
}
};
:\xiaogw\h\gameset.h(86) : error C2512: 'A' : no appropriate default constructor available
c:\xiaogw\h\gameset.h(87) : fatal error C1903: unable to recover from previous error(s); stopping compilation
WinMain.cpp
c:\xiaogw\h\gameset.h(86) : error C2512: 'A' : no appropriate default constructor available
c:\xiaogw\h\gameset.h(87) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.MyGame.exe - 4 error(s), 0 warning(s)大哥
我这是错在哪里啊?
谢谢!
.h文件呢,没什么特别的用处,就是用来声明用的,它不会被编译成obj,所以最好在这里不要用任何的非声明性质的代码,比如初始化,函数体(类里的会成为inline)等.
.cpp在同名的h 文件指导下 会生成成为.obj, 最后连接这些obj.
你的B文件里只有class A的声明..而并没有class a的构造函数,它是在a.h中说明的,你就要把a.h给包含了就可以了.