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();
    }    
}
2
  • stackoverflow.com/questions/12573816/… Commented Mar 28, 2017 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 Commented Mar 29, 2017 at 0:02

2 Answers 2

1

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

float Company::budget;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, I haven't noticed that
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:(
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.
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;

1 Comment

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:(.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.