ERROR NO.2
内置函数inline。类体中定义的函数功能一般规模较小,系统调用类为定义的函数的过程需要的时间开销是比较大的,为了减少系统调用函数的时间开销,如果在类体智能光定义的函数不包括循环等控制结构,C++会自动将它们作为内置函数。
内置函数的作用是将函数实现的代码替代声明的地方,以减小系统调用类外函数的时间开销。
今天使用是遇到了问题,代码如下
1//head.h 2#include <iostream> 3#include <string> 4using namespace std; 5 6class Student 7{ 8public: 9 inline void Display(); 10 inline void set_vlaue(); 11protected: 12private: 13 int num; 14 string name; 15 char sex; 16}; 17//main.cpp 18#include "head.h" 19using namespace std; 20int main() 21{ 22 Student stu; 23 stu.set_vlaue(); 24 stu.Display(); 25 return 0; 26} 27//student.cpp 28#include "head.h" 29 30void Student::set_vlaue() 31{ 32 cin >> num; 33 cin >> name; 34 cin >> sex; 35} 36 37void Student::Display() 38{ 39 cout << "num:" << num << endl; 40 cout << "name:" << name << endl; 41 cout << "sex:" << sex << endl; 42}
在编译是出现如下错误:
1>mian_s3_3.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Student::Display(void)" (?Display @Student @@QAEXXZ),该符号在函数 _main 中被引用
1>E:\VCworkspace\C++_exercise\lx_3\Debug\shiyan3_3.exe : fatal error LNK1120: 1 个无法解析的外部命令
解决方法:
仔细检查发现inline函数使用时函数实现必须也在定义类的文件中。