注册 登录
编程论坛 C++教室

关于类先使用后定义

全世界安静 发布于 2012-07-08 21:12, 425 次点击
#include <iostream>
using namespace std;
class test;
void main(){
    test my;
    my.show();
}
class test{
public:
   int x;
   test(int a=0){
      x=a;
    }
   void show(){
       cout<<x<<endl;
    }
};   
编译出错,error C2079: 'my' uses undefined class 'test'
求解释,感谢!   
3 回复
#2
rjsp2012-07-09 08:16
当编译到 test my; 时,编译器只知道它是个class,不知道其大小,怎么给其分配栈空间?
当编译到 my.show(); 时,编译器只知道它是个class,怎么知道它有没有show成员?
#3
m21wo2012-07-09 11:35
编译器不知道给my 分配多少内存
#4
long00422012-07-09 12:54
类的前置申明是用来在定义另外的类的时候用的, 而且只能申明指针, 不能申明为对象。
1