我的系统是Ubuntu18,codeblocks IDE,在复习C++,本来编译是没有问题的,更改头文件后出现问题,编译器貌似无视了我对头文件和类外定义文件的修改,检查target没有问题。代码如下,应该是没有问题的:
main文件:
#include <iostream>
#include "Box.h"
using namespace std;
int main(){
Box box1;
Box box2{10,10,10,10};
Box box3{10};
Box box4{10,10};
box1.test();
cout<<box1.volume()<<endl;
cout<<box2.volume()<<endl;
// cout<<box4.get_count()<<endl;
}
Box.h 头文件
#ifndef BOX_H
#define BOX_H
class Box
{
public:
Box();
Box(double lv,double wv,double hv,double n);
explicit Box(double side);
Box(int i,int n);
double volume();
void test();
int get_count();
private:
double length{1};
double width{1};
double height{1};
static int object_count;
};
#endif // BOX_H
Box.cpp文件
#include "Box.h"
#include <iostream>
using namespace std;
int Box::object_count {};
Box::Box()
{
cout<<"no index construction function"<<endl;
//默认的无参构造函数一旦指定,就不会额外创建
++object_count;
cout<<"object_count="<<object_count<<endl;
}
Box::Box(double side):Box(side,side,side,side)
{
cout<<"beg construction function"<<endl;
++object_count;
}
Box::Box(double lv,double wv,double hv,double n):length{lv},width{wv},height{hv}
{
cout<<"three index construction function"<<endl;
++object_count;
}
Box::Box(int i,int j){
cout<<"int construction function"<<endl;
++object_count;
}
void Box::test(){
cout<<"test"<<endl;
}
double Box::volume(){
return length*width*height;
cout<<"??"<<endl;
}
int Box::get_count(){
return object_count;
}
刚开始我试着加入静态数据成员object_count,报错找不到get_count()函数,于是加入了test()函数进行测试,保存编译后仍然没用,报错如下:
-------------- Build: Debug in Class_review (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -g -Iinclude -c /home/tianrui/workspace/C++/Beginning_Learn/Class_review/main.cpp -o obj/Debug/main.o
/home/tianrui/workspace/C++/Beginning_Learn/Class_review/main.cpp: In function ‘int main()’:
/home/tianrui/workspace/C++/Beginning_Learn/Class_review/main.cpp:11:10: error: ‘class Box’ has no member named ‘test’
box1.test();
^~~~
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
我测试了很多次,更改Box.cpp与Box.h文件后Rebulid根本毫无反应!!
target里明明都选上了。。