33,311
社区成员
发帖
与我相关
我的任务
分享
#ifndef BOX_H
#define BOX_H
class Box
{
public:
Box(double l=1.0,double w=1.0,double h=1.0);
double volume();
private:
double length;
double width;
double height;
};
#endif
Box.cpp
#include <iostream>
#include "Box.h"
using namespace std;
Box::Box(double l,double w,double h):length(l),width(w),height(h) {
cout<<"constructor called!"<<endl;
if(length<=0.0)
length=1.0;
if(width<=0.0)
width=1.0;
if(height<=0.0)
height=1.0;
}
double Box::volume() {
return length*width*height;
}
Box_12.1.cpp
#include<iostream>
#include "Box.h"
using namespace std;
int main(){
Box firstBox(2.2,1.1,0.5);
Box secondBox;
Box* pthirdBox=new Box(15.0,20.0,8.0);
cout<<"The volume of the first box is "
<<firstBox.volume()
<<endl;
cout<<"The volume of the second box is "
<<secondBox.volume()
<<endl;
cout<<"The volume of the third box is "
<<pthirdBox->volume()
<<endl;
delete pthirdBox;
return 0;
}
上面几个文件的代码是复制的lz的代码,VS2008 + Win32 Console编译通过,没有发现错误。
下面是编译产生的输出:
1>------ Rebuild All started: Project: Test, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Test', configuration 'Debug|Win32'
1>Compiling...
1>Box.cpp
1>Box_12.1.cpp
1>Generating Code...
1>Linking...
1>LINK : F:\Study\C++\Test\Debug\Test.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Build log was saved at "file://f:\Study\C++\Test\Test\Debug\BuildLog.htm"
1>Test - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
不是编译环境弄得有问题,我是想不到什么原因了,呵呵。
还是建议LZ你检查下有没有生成Box.obj文件。你把文件放在一个工程里面了吗?
Box.cpp是不是没有被编译到啊? 个人建议,仅供参考。