为什么以下的C++ code有compile error?
Test_Class.cpp
==============================
class CRectangle{
int *width, *height;
public:
CRectangle (int a, int b){
width= new int;
height = new int;
*width = a;
*height = b;
}
~CRectangle(){
delete width;
delete height;
}
int area(){
return ((*width)*(*height));
}
};
=================================
Do_Factorial.cpp
=================================
#include<iostream>
class CRectangle;
int main()
{
CRectangle c(2,3);
std::cout<<c.area()<<'\n';
}
=================================
compile error 是:
g++ do_factorial.cpp test_class.cpp -o do_factorial
do_factorial.cpp: In function `int main()':
do_factorial.cpp:9: variable `CRectangle c' has initializer but incomplete type
==============================
class CRectangle{
int *width, *height;
public:
CRectangle (int a, int b){
width= new int;
height = new int;
*width = a;
*height = b;
}
~CRectangle(){
delete width;
delete height;
}
int area(){
return ((*width)*(*height));
}
};
=================================
Do_Factorial.cpp
=================================
#include<iostream>
class CRectangle;
int main()
{
CRectangle c(2,3);
std::cout<<c.area()<<'\n';
}
=================================
compile error 是:
g++ do_factorial.cpp test_class.cpp -o do_factorial
do_factorial.cpp: In function `int main()':
do_factorial.cpp:9: variable `CRectangle c' has initializer but incomplete type