Why can't I use inline functions with my setter method in c++. Here is the code:

Main.cpp

#include <iostream>
#include "Employee.h"
using namespace std;

int main()
{

    Employee h;
    h.setAge(30);
    h.setSalary(70000);
    h.setyearsOfService(5);
    h.printAllInfo();

    return 0;
}

Employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H


class Employee
{
    public:
        inline int getAge();
        int setAge(int n);
        inline int getyearsOfService();
        int setyearsOfService(int n);
        inline int getSalary();
        inline int setSalary(int n);
        void printAllInfo();

    private:
        unsigned int age;
        unsigned int yearsOfService;
        unsigned int Salary;


};

#endif // EMPLOYEE_H

Employee.cpp

#include "Employee.h"
#include<iostream>

int Employee::getAge()
{
    return age;
}

int Employee::setAge(int n)
{
    age = n;
    return age;
}

int Employee::getSalary()
{
    return Salary;
}

int Employee::setSalary(int n)
{
    Salary = n;
    return Salary;
}

int Employee::setyearsOfService(int n)
{
    yearsOfService = n;
    return yearsOfService;
}

int Employee::getyearsOfService()
{
    return yearsOfService;
}

void Employee::printAllInfo()
{
    using std::cout;
    using std::endl;

    cout << "The employee is " << getAge() << " year old and he has worked for " << getyearsOfService() << " years and his/her salary is " << getSalary() << " smakroos" << endl;

}

Here is the build log I get when I try to compile:

Day6Programming/main.cpp:10: undefined reference to `Employee::setSalary(int)'

Why am I getting this error?

What compiler are you using?

I am using Code::Blocks IDE, also I am running Ubuntu 12.04

If a function is declared inline, its definition (implementation) must appear in the header file. Here are three equivalent options:

// in foo_header_option1.h
#ifndef FOO_HEADER_OPTION1_H
#define FOO_HEADER_OPTION1_H

class Foo {
  private:
    int bar;
  public:
    int getBar() const { return bar; };
    void setBar(int aBar) { bar = aBar; };
};

#endif

Or

// in foo_header_option2.h
#ifndef FOO_HEADER_OPTION2_H
#define FOO_HEADER_OPTION2_H

class Foo {
  private:
    int bar;
  public:
    inline int getBar() const; 
    inline void setBar(int aBar); 
};

int Foo::getBar() const {
  return bar;
};

void Foo::setBar(int aBar) {
  bar = aBar;
};

#endif

Or

// in foo_header_option2.h
#ifndef FOO_HEADER_OPTION2_H
#define FOO_HEADER_OPTION2_H

class Foo {
  private:
    int bar;
  public:
    int getBar() const; 
    void setBar(int aBar); 
};

inline int Foo::getBar() const {
  return bar;
};

inline void Foo::setBar(int aBar) {
  bar = aBar;
};

#endif

So, in all cases, the definition must appear in the header file. This makes sense because if the compiler is going to actually inline the function's code (put the function's code at the call-site, replacing the actual function call), then it must be able to grab that source code, and because each cpp file is compiled separately, when it compiles the caller's code, it must also have access to the function's code, thus, in the header file where it was declared.

In technical terms, a function marked as inline will have internal linkage which means that it will not be part of the exported symbols of a compiled object file, thus, unavailable to the linker, thus, resulting in an "undefined reference" error if you try to call it from anywhere outside the cpp file in which it is defined. The way to solve the problem is to make sure the function's definition is present in each cpp file that uses it, and the easiest way to do that is to put the definition in the header file that declares it (either fusing the declaration-definition, or putting the declaration and definition separately but in the same header file, as examplified above).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.