2016-07-12 6 views
0

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!

+0

'void print (Zeichenfolgename)' -> 'void Mitarbeiter :: print (Zeichenfolgename)' in employee.cpp –

+0

Ihre Frage hat nichts mit Compileraufbau oder Kompilierungsfehlern zu tun. – EJP

Antwort

1

void print(string name) ist in der Klasse Employee nicht definiert, daher klagt Ihr Linker, dass er ihn nicht finden kann.

sollten Sie den Code ändern zu:

void Employee::print(string name) { 
    ... 
} 

Dann ist es definiert und Linker wird diese Funktion finden.

By the way, da Sie nur die Zeichenfolge gedruckt wird, ist es besser const referense passieren, so ist es besser, wie unten schreiben:

void Employee::print(const string& name) { 
    ... 
} 

Aber es ist auch nicht gut, weil print Funktion Employee ist ' s Mitglied Funktion, es weiß, welche Variable zu drucken, so ist es besser, Ihren Code zu ändern:

Dann macht es Sinn.