C++ 修复链接器 [错误]
C++ Fixing Linker [Error]
我仍在学习如何在头文件中使用 类,我 运行 遇到了一个问题。无论出于何种原因,当我转到 运行 程序时出现错误 "V3_Employee.cpp:(.text+0x3e): undefined reference to `Employee::print(std::string)'"。
我认为这与编译器与 .o 文件或类似文件的交互有关。
main.cpp
#include <iostream>
#include <string>
#include "V3_Employee.h" //Including header file of for class Burrito
using namespace std;
int main()
{
Employee Sean("Sean");
return(0);
}
Employee.h
//Header guard
#ifndef V3_EMPLOYEE_H //If this header has not already been included in main.cpp
#define V3_EMPLOYEE_H //Then include the following lines of code
#include <string>
using namespace std;
class Employee //Creating a class named 'Employee'
{
private:
//Creating variables
string m_name;
//Creating a public interface
public:
//Creating a Construct
Employee(string m_name);
//Creating a 'Member function', another name for a function inside a class
void print(string m_name);
};
endif //代码结束
Employee.cpp
#include "V3_Employee.h"
#include <iostream>
#include <string>
using namespace std;
Employee::Employee(string m_name)
{
print(name);
}
void print(string name) //Defining the function 'print'
{
cout<<"Name: "<<m_name<<endl;
}
我还有另一个几乎完全相同的代码,而不是使用整数输入而不是字符串:
main2.cpp
#include <iostream>
#include "V2_Burrito.h" //Including header file of for class Burrito
using namespace std;
int main()
{
Burrito Test(1); //Setting 'Test' as an object of class 'Burrito' with an input of '1'
return(0);
}
Burrito.h
//Header guard
#ifndef V2_BURRITO_H //If this header has not already been included in main.cpp
#define V2_BURRITO_H //Then include the following lines of code
class Burrito //Creating a class named 'Burrito'
{
//Creating a public interface
public:
//Creating a 'Constructor', or a way to manipulate 'private' data
Burrito(int a); //This constructor contains 1 input in the form of an integer
//Creating a 'Member function', another name for a function inside a class
void setType(int a);
};
#endif //End of code
感谢您提供的任何帮助!
void print(string name)
未在 class Employee
中定义,因此您的链接器抱怨找不到它。
您应该将代码更改为:
void Employee::print(string name) {
...
}
然后它被定义,链接器会找到这个函数。
顺便说一句,因为你只是打印字符串,所以最好通过const referense
,所以最好像下面这样写:
void Employee::print(const string& name) {
...
}
但是也不好,因为print
函数是Employee
的成员函数,它知道要打印哪个变量,所以最好把你的代码改成:
void Employee::print() {
cout<<"Name: "<<m_name<<endl;
}
那就有道理了
我仍在学习如何在头文件中使用 类,我 运行 遇到了一个问题。无论出于何种原因,当我转到 运行 程序时出现错误 "V3_Employee.cpp:(.text+0x3e): undefined reference to `Employee::print(std::string)'"。
我认为这与编译器与 .o 文件或类似文件的交互有关。
main.cpp
#include <iostream>
#include <string>
#include "V3_Employee.h" //Including header file of for class Burrito
using namespace std;
int main()
{
Employee Sean("Sean");
return(0);
}
Employee.h
//Header guard
#ifndef V3_EMPLOYEE_H //If this header has not already been included in main.cpp
#define V3_EMPLOYEE_H //Then include the following lines of code
#include <string>
using namespace std;
class Employee //Creating a class named 'Employee'
{
private:
//Creating variables
string m_name;
//Creating a public interface
public:
//Creating a Construct
Employee(string m_name);
//Creating a 'Member function', another name for a function inside a class
void print(string m_name);
};
endif //代码结束
Employee.cpp
#include "V3_Employee.h"
#include <iostream>
#include <string>
using namespace std;
Employee::Employee(string m_name)
{
print(name);
}
void print(string name) //Defining the function 'print'
{
cout<<"Name: "<<m_name<<endl;
}
我还有另一个几乎完全相同的代码,而不是使用整数输入而不是字符串:
main2.cpp
#include <iostream>
#include "V2_Burrito.h" //Including header file of for class Burrito
using namespace std;
int main()
{
Burrito Test(1); //Setting 'Test' as an object of class 'Burrito' with an input of '1'
return(0);
}
Burrito.h
//Header guard
#ifndef V2_BURRITO_H //If this header has not already been included in main.cpp
#define V2_BURRITO_H //Then include the following lines of code
class Burrito //Creating a class named 'Burrito'
{
//Creating a public interface
public:
//Creating a 'Constructor', or a way to manipulate 'private' data
Burrito(int a); //This constructor contains 1 input in the form of an integer
//Creating a 'Member function', another name for a function inside a class
void setType(int a);
};
#endif //End of code
感谢您提供的任何帮助!
void print(string name)
未在 class Employee
中定义,因此您的链接器抱怨找不到它。
您应该将代码更改为:
void Employee::print(string name) {
...
}
然后它被定义,链接器会找到这个函数。
顺便说一句,因为你只是打印字符串,所以最好通过const referense
,所以最好像下面这样写:
void Employee::print(const string& name) {
...
}
但是也不好,因为print
函数是Employee
的成员函数,它知道要打印哪个变量,所以最好把你的代码改成:
void Employee::print() {
cout<<"Name: "<<m_name<<endl;
}
那就有道理了