|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 肚子饿了 于 2012-8-7 20:25 编辑
我最近刚开始学习C++,今天自己参考一本书写了个小程序
代码如下:
- //定义头文件Box.h
- #ifndef BOX_H
- #define BOX_H
- class Box
- {
- public:
- double length;
- double breadth;
- double height;
- Box(double lengthValue,double breadthValue,double heightValue);
- double volume();
- };
- #endif
- //定义类中的成员函数,放在Box.cpp文件中
- #include "Box.h"
- Box::Box(double lengthValue,double breadthValue,double heightValue)
- {
- length=lengthValue;
- breadth=breadthValue;
- height=heightValue;
- }
- double Box::volume()
- {
- return length*breadth*height;
- }
- //在第三个文件中计算box的体积
- #include "Box.cpp"
- #include <iostream>
- int main()
- {
- Box firstBox(80.0,50.0,40.0);
- double firstBoxVolume=firstBox.volume();
- std::cout<<std::endl;
- std::cout<<"volume of first box object is "<<firstBoxVolume<<std::endl;
- return 0;
- }
复制代码
能正常编译
但是链接出错:
错误提示如下:
1>Boxvolume.obj : error LNK2005: "public: __thiscall Box::Box(double,double,double)" (??0Box@@QAE@NNN@Z) 已经在 Box.obj 中定义
1>Boxvolume.obj : error LNK2005: "public: double __thiscall Box::volume(void)" (?volume@Box@@QAENXZ) 已经在 Box.obj 中定义
1>E:\实践程序\C++初识\Debug\C++初识.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
我觉得没有问题啊,这本书上好像也是这么写的。
经过度娘的帮助,我把第三个文件中的#include "Box.cpp"改成#include "Box.h"就不会出现多重定义符号的问题了
|
|