[Solved] In Dev-c++, the header file and the header file function are separated, and the problem of compiling the main function jumps out of undefined reference to solve the problem

Update time: 2022.5.13 8:00

Learning Tan Haoqiang C++ 3rd Edition Object-Oriented Programming, Chapter 2 Exercise 4:

Three files need to be separated, the main function (.cpp), the class declaration (header file), and the member function definition file (.cpp)

In the implementation of Dev-C++ alone, it is found that undefined reference to set_value keeps appearing in the compilation, which means that the function we defined is undefined, but we have defined it, so it should be that we are not unable to Link to the function implementation file.

Solution 1: Compile with the visual studio 2022 compiler

Source code:

Class declaration:

#include<iostream>
#include<string.h>
#include<string>
using namespace std;
//Student.h
class Student
{
private:
int num;
string name;
char sex;
public:
void set_value();
void show_value();
};

member function definition

#include<iostream>
#include"class.h"
using namespace std;
void Student::set_value()
{
cin >> num >> name >> sex;
}
//compile this file first
void Student::show_value()
{
cout << "num:" << num << "name:" << name << "sex:" << sex;
}

Main function: (special explanation: #define in the first paragraph of the main function is to use printf and scanf (visual stdio 2022 thinks it is unsafe and cannot be used, so this macro definition needs to be introduced))

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include"class.h"

using namespace std;

int main()
{
Student s1, s2;
s1.set_value();
s2.set_value();
s1.show_value();
s2.show_value();


return 0;
}

problem solved:

Solution two:

Because in dev-c++, the system is a file-by-file search, that is to say, if you want to use this class or this function, you need to import the file that defines the implementation of the function or the file that declares the class, such as we are in When using cout and cin functions to extract and insert streams for input and output, the preprocessing instruction #include needs to be used to introduce input and output stream functions.

So in dev-c++ the files should be imported sequentially. The code is as follows:

1. Member function definition file define.cpp

#include<iostream>
#include"class.h"
using namespace std;
void Student::set_value()
{
cin >> num >> name >> sex;
}
//compile this file first
void Student::show_value()
{
cout << "num: " << num << "name: " << name << "sex: " << sex<<endl;
}

2. Class declaration file class.h

#include<iostream>
#include<string>
using namespace std;

class Student
{
private:
int num;
string name;
char sex;
public:
void set_value();
void show_value();
};

3. The main function file main.cpp (.cpp is introduced here, while Vscode introduces class.h

#include"define.cpp"
#include<iostream>
using namespace std;

int main()
{
Student s1, s2;
s1.set_value();
s2.set_value();
s1.show_value();
s2.show_value();


return 0;
}

The compilation and running results are shown in the figure:

Let’s analyze:

The main function here introduces the define.cpp file, which is equivalent to inserting the define.cpp function implementation file into main.cpp, and introducing the class declaration file class.h into the define.cpp file, which is quite The class.h function is inserted into main.cpp, so it is equivalent to three files combined.

Summary:

In dev-c++, it is a file-by-file search, which needs to be introduced if the corresponding file function needs to be used. In visual studio 2022, creating a file is a project. In the introduction header file, if there is a function declaration in the header file, then in the header file, the powerful linking function of vscode will automatically find the corresponding function implementation file (only in the search in the current directory).