为什么以下的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
[Yahoo! (12-16 9:30, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
java用多了吧#include<iostream>
class CRectangle;
int main()
{
CRectangle c(2,3);
std::cout<<c.area()<<'\n';
}
从个文件来看,没有CRectangle的定义,当然没办法compile,简单一点,#include "Test_Class.cpp"[hula (12-19 12:10, Long long ago)]
[ 传统版 |
sForum ][登录后回复]2楼
(引用 hula:java用多了吧#include class CRectangle; int main() { CRectangle c(2,3); std::cout...)haha, java syndrome[sarah (12-19 23:36, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼