1

I keep getting error: undefined reference to 'Company::budget'. My method is set so as to take the value of company's (any created) budget and subtract Employees' salary from it. I keep getting this problem. Tried both pointers and let's say "normal calling". Ok, there's the code snippet: (rest of it works)

company.h

#include <iostream>
#include <cstdlib>
#include <list>
#include <vector>
#include "employee.h"
using namespace std; 

class Company
{
public:
    Company* comp;
    void  hire(Employee& emp, float putSalary);
    void  fire(Employee& emp);
    void  endOfMonth(Company& comp);
    Company(float);
 //   static float moneyamount;
private:
    static float budget;
    vector <Employee>* Employees;   
};

company.cpp

void Company::endOfMonth(Company& comp)
{
    for (iterat=0; iterat < Employees->size() ; iterat++)
    {

        cout << (*Employees)[iterat].fullName << endl;
        cout << (*Employees)[iterat].getSalary() << endl;
        comp.budget = comp.budget - (*Employees)[iterat].getSalary();
    }    
}
Anton K
  • 4,658
  • 2
  • 47
  • 60
Lutrin
  • 11
  • 1
  • http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574407#12574407 – chris Mar 28 '17 at 23:30
  • if it is possible you should be using `smart_pointers` and also it is not a great idea to be using `using namepsace std` – Matthew Mar 29 '17 at 00:02

2 Answers2

1

You are missing the definition of the static class data member. Add the following line to the file company.cpp:

float Company::budget;
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Thanks a lot, I haven't noticed that – Lutrin Mar 28 '17 at 23:31
  • 1
    I would have said 'take the static off'. Why should the budget of a company be static? It effectively restricts the app to handing only one company:( Also, you have to remember the annoying static defn/initializer:( – ThingyWotsit Mar 28 '17 at 23:32
  • 1
    Yeah, I agree. A company *has* a budget. It belongs to that company. All companies do not share a single budget, which is what static means. Even if it works now, limiting yourself to one means more work later if you ever decide to expand the use of the code. BTW the reason for the separate static member variable is that they need to be allocated somewhere (all variables need to exist in some module somewhere) and they're not part of any *single* object. – Jason Lang Mar 28 '17 at 23:55
1

Static class members variables are static over all instances of a class. So if you have two instances of one class, they share the static variable. Also, these variables are also valid even when there is no instance of the class. So, static member functions may use static member variables. That is the reason why they must defined somewhere outside the class in the object file.

You define it and reserve the necessary space for it in memory at the top level of you .cpp file:

float Company::budget;
cmks
  • 527
  • 2
  • 11
  • 1
    It seems to be a common problem in C++, devs just seem to type 'static' at random, for no aparrent reason. In this case, it restricts the app to one company, but the devs do it in multithreaded and other environments where it's either restricting or dangerous:(. – ThingyWotsit Mar 28 '17 at 23:41