关于inline error LNK1120: 3 unresolved externals

sycocacola 2014-03-07 03:48:45
头文件
#ifndef BOX_H
#define BOX_H
class Box
{
public:
Box(double l=1.0,double w=1.0,double h=1.0);
double volume();
double getlength();
double getwidth();
double getheight();
~Box();

private:
double length;
double width;
double height;
};
#endif


源文件
#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;
}
Box::~Box(){
cout<<"Destructor called!"<<endl;
}
double Box::volume(){
return height*width*height;
}
inline double Box::getlength(){
return length;
}
inline double Box::getwidth(){
return width;
}
inline double Box::getheight(){
return height;
}

运行部分
#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 size of the first box is "
<<firstBox.getlength()<<" by "
<<firstBox.getwidth()<<" by "
<<firstBox.getheight()
<<endl
<<"volume of the first Box is "<<firstBox.volume()
<<endl
<<"volume of the second Box is "<<secondBox.volume()
<<endl
<<"volume of the third Box is "<<pthirdBox->volume()
<<endl;
delete pthirdBox;
return 0;
}

问题就是:
--------------------Configuration: Box124 - Win32 Debug--------------------
Compiling...
Skipping... (no relevant changes detected)
124.cpp
Linking...
124.obj : error LNK2001: unresolved external symbol "public: double __thiscall Box::getlength(void)" (?getlength@Box@@QAENXZ)
124.obj : error LNK2001: unresolved external symbol "public: double __thiscall Box::getwidth(void)" (?getwidth@Box@@QAENXZ)
124.obj : error LNK2001: unresolved external symbol "public: double __thiscall Box::getheight(void)" (?getheight@Box@@QAENXZ)
Debug/Box124.exe : fatal error LNK1120: 3 unresolved externals
执行 link.exe 时出错.

Box124.exe - 1 error(s), 0 warning(s)
编译没问题,连接报错。我是在类定义的外部来实现的啊,运用inline,使之成为内联函数,减少开销。为什么会在链接的时候出现这个问题呢?
...全文
164 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-03-08
  • 打赏
  • 举报
回复
1) 类外inline成员函数一般是多余的
引用 1 楼 greatchina01 的回复:
成员函数自动inline
++ 1)类的inline成员函数,如果非要写在函数外部的话, 一定要写在头文件里,或者类似头文件的文件里,并且要一起包含到,用到他们的源文件中 除非只有一个文件用到这个类.
greatchina01 2014-03-07
  • 打赏
  • 举报
回复
成员函数自动inline

33,311

社区成员

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

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