简体   繁体   中英

C++ Fixing Linker [Error]

I am still learning how to use classes in header files, and I'm running into a problem. For whatever reason, I'm getting the error "V3_Employee.cpp:(.text+0x3e): undefined reference to `Employee::print(std::string)'" when I go to run the program.

I believe it has something to do with the compiler's interaction with the .o file or something of that sort.

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 //End of 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; 
    }

I also have another code that is almost exactly the same, instead using an integer input instead of a string:

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

I appreciate any help you might be able to offer!

void print(string name) is not defined in class Employee , so your linker complains it can not find it.

You should change the code to:

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

Then it's defined and linker will find this function.

By the way, since you're only printing the string, it's better to pass const referense , so it's better to write like below:

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

But it's not good either, because print function is Employee 's member function, it knows which variable to print, so it's better to change your code to:

void Employee::print() {
   cout<<"Name: "<<m_name<<endl;
}

Then it makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM