Я все еще учась использовать классы в файлах заголовков, и у меня возникает проблема. По какой-то причине я получаю сообщение об ошибке «V3_Employee.cpp :(. Text + 0x3e): неопределенная ссылка на« Employee :: print (std :: string) », когда я иду запускать программу.C++ Fixing Linker [Ошибка]
Я считаю, что это как-то связано с взаимодействием компилятора с файлом .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)' -> 'void Employee :: print (string name)' in employee.cpp –
Ваш вопрос не имеет ничего общего с ошибками сборки компилятора или компиляции. – EJP