> 文档中心 > Dev-c++中将头文件和头文件函数分离,编译主函数跳出undefined reference to 的问题解决

Dev-c++中将头文件和头文件函数分离,编译主函数跳出undefined reference to 的问题解决

在学习谭浩强c++第三版面向对象编程,第二章习题四中:

需要实现三个文件,主函数(.cpp),类的声明(头文件),对成员函数定义文件(.cpp)

源代码:

类的声明:

#include#include#includeusing namespace std;//Student.hclass Student{private:int num;string name;char sex;public:void set_value();void show_value();};

成员函数定义

#include#include"类.h"using namespace std;void Student::set_value(){cin >> num >> name >> sex;}//先编译此文件 void Student::show_value(){cout << "num:" << num << "name:" << name << "sex:" << sex;}

主函数:

#define _CRT_SECURE_NO_WARNINGS#include #include"类.h"using namespace std;int main(){Student s1, s2;s1.set_value();s2.set_value();s1.show_value();s2.show_value();return 0;}

第一段的#define 是为了使用printf和scanf(visual stdio 2022 认为其不安全不能使用,需要引入这个宏定义)

在使用Dev-C++实现中,发现无法在编译一直出现undefined reference to set_value,也就是提示我们定义这个函数,也就是没有链接到,我不清楚他的链接机制,编译这个成员函数定义的文件也没有.obj文件,考虑到dev-c++编译器可能功能不强大, 我使用visual stdio 2022敲一样的代码,编译成功,可以运行。问题解决