0

i am a beginner programmer currently learning about classes and objects, for some reason my code is getting the error "undefined reference to " whenever i try to run the code. This is testemployee.cpp

#include "employee.h"
#include <iostream>
using namespace std;
int main()
{
    employee one;
    one.modifyfirstname("kevin");
    one.modifylastname("Patel");
    cout<<one.returnfirstname()<<one.returnlastname();
}

This is employee.cpp

#include "employee.h"
employee::employee()
{
    id=0;
    firstname="";
    lastname="";
    birth="";
    address="";
    yearhired=0;
    salary=0.0;
    areacode=0;
    telephone="";
}
int employee::returnid()
{
    return id;
}
string employee::returnfirstname()
{
    return firstname;
}
void employee::modifyfirstname(string firstname1)
{
    firstname=firstname1;
}
string employee::returnlastname()
{
    return lastname;
}
void employee::modifylastname(string lastname1)
{
    lastname=lastname1;
}

This is employee.h

#include <string>
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
using namespace std;
class employee
{
    private:
        int id;
        string firstname;
        string lastname;
        string birth;
        string address;
        int yearhired;
        double salary;
        int areacode;
        string telephone;
    public:
        employee();
        int returnid();
        string returnfirstname();
        void modifyfirstname(string);
        string returnlastname();
        void modifylastname(string);


};
#endif

the error /usr/bin/ld: /tmp/ccqnCjc9.o: in function main': testemployee.cpp:(.text+0x26): undefined reference to employee::employee()' /usr/bin/ld: testemployee.cpp:(.text+0x6e): undefined reference to employee::modifyfirstname(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' /usr/bin/ld: testemployee.cpp:(.text+0xd4): undefined reference to employee::modifylastname(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' /usr/bin/ld: testemployee.cpp:(.text+0x10b): undefined reference to employee::returnfirstname[abi:cxx11]()' /usr/bin/ld: testemployee.cpp:(.text+0x140): undefined reference to employee::returnlastnameabi:cxx11' collect2: error: ld returned 1 exit status

i've tried to compile my code

  • 1
    It looks like you are not linking `employee.o` or a library containing it to the `testemployee` program. It is not (usually) enough just for the source file to `#include` the header. – John Bollinger Mar 24 '22 at 21:55
  • Show your exact compile command. Also if you are using VSCode the error can easily happen since the default behavior of the c++ support in the IDE is to build only the active file. The documentation explains what change to make to the tasks.json to build more than 1 source file. – drescherjm Mar 24 '22 at 23:11

0 Answers0