error LNK2019: 无法解析的外部符号

sycocacola 2014-03-07 02:31:11
头文件Box.h
#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;
}

语法应该没问题,跑出来就报:
1>Box_12.1.obj : error LNK2019: 无法解析的外部符号 "public: double __thiscall Box::volume(void)" (?volume@Box@@QAENXZ),该符号在函数 _main 中被引用
1>Box_12.1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Box::Box(double,double,double)" (??0Box@@QAE@NNN@Z),该符号在函数 _main 中被引用
1>C:\Users\Administrator.WIN-VRV2JV05SFO\Documents\Visual Studio 2010\Projects\Example12.1\Debug\Example12.1.exe : fatal error LNK1120: 2 个无法解析的外部命令

在网上看了一下,有的说函数没有实现,但是在Box.cpp文件中已经实现了啊,有的说没有把.lib文件包含进来,但是也没有用到其他的库啊! 很费解,换其他程序也会这样报错。不知道是怎么回事?
...全文
207 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-03-08
  • 打赏
  • 举报
回复
MS的工程(VC6.dsp),项目(VC 7以后 .vcproj)实际上就是 makefile 的某种封装或者转换 如果,没有把源文件加入到工程,项目中,就就像 makefile中不存在,编译,链接源文件的指令一样 那么Bulid的时候,.obj 就不会生成,工程,项目编译,链接就会失败,找不到源文件中定义的函数.
lm_whales 2014-03-08
  • 打赏
  • 举报
回复
估计是,没有把.cpp 加入到工程里面,查看一下工程里面,源文件部分,有没有 box.cpp 这个程序里Box类的两个文件,应该不是直接创建的工程,然后添加类生成的. 而是别的地方,编辑好的,直接拷贝过来的.
JiMoKuangXiangQu 2014-03-07
  • 打赏
  • 举报
回复
引用 5 楼 sycocacola 的回复:
引用 2 楼 czarten 的回复:
你把文件放在一个工程里面了吗?
必须的啊,都是一个在一个项目里的头文件,源文件
Box.h
#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文件。
sycocacola 2014-03-07
  • 打赏
  • 举报
回复
引用 2 楼 czarten 的回复:
你把文件放在一个工程里面了吗?
必须的啊,都是一个在一个项目里的头文件,源文件
sycocacola 2014-03-07
  • 打赏
  • 举报
回复
引用 1 楼 JiMoKuangXiangQu 的回复:
Box.cpp是不是没有被编译到啊? 个人建议,仅供参考。
这个。。。不可抗啊
赵4老师 2014-03-07
  • 打赏
  • 举报
回复
重建所有 试试
你怎么了熊吉 2014-03-07
  • 打赏
  • 举报
回复
你把文件放在一个工程里面了吗?
JiMoKuangXiangQu 2014-03-07
  • 打赏
  • 举报
回复
Box.cpp是不是没有被编译到啊? 个人建议,仅供参考。

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧