Ich lerne immer noch, wie man Klassen in Header-Dateien verwendet, und ich bin auf ein Problem stoßen. Aus irgendeinem Grund erhalte ich den Fehler "V3_Employee.cpp :(. Text + 0x3e): undefinierter Verweis auf" Employee :: print (std :: string) "", wenn ich das Programm starte.C++ Fixing Linker [Fehler]
Ich glaube, es hat etwas mit der Interaktion des Compilers mit der .o-Datei oder etwas dieser Art zu tun.
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 // Ende der Code
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;
}
Ich habe auch einen anderen Code, ist fast genau das Gleiche, in Statt eine ganze Zahl Eingabe anstelle einer Zeichenfolge mit:
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
ich jede Hilfe dankbar Sie könnten in der Lage sein zu bieten!
'void print (Zeichenfolgename)' -> 'void Mitarbeiter :: print (Zeichenfolgename)' in employee.cpp –
Ihre Frage hat nichts mit Compileraufbau oder Kompilierungsfehlern zu tun. – EJP