关于inline error LNK1120: 3 unresolved externals
头文件
#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,使之成为内联函数,减少开销。为什么会在链接的时候出现这个问题呢?