samedi 25 juin 2016

Getting undefined reference error while using friend function [duplicate]

This question already has an answer here: What is an undefined reference/unresolved external symbol error and how do I fix it? 25 answers I am learning C++ and wrote a program to understand the friend functions. I keep getting this error when i compile. I ahve spent lot of time trying to debug this and search for similar questions, but could not find anything. Desperately need some help. The Error I get is "undefined reference to Budget::corpBudget" The program has 2 .h files and 2 cpp files. AuxOffice.h Budget.h AuxOffice.cpp budget.cpp I used this command to compile g++ AuxOffice.cpp budget.cpp -o demo this is the source code fro all the files: First File : Budget.h #ifndef BUDGET_H #define BUDGET_H #include "AuxOffice.h" using namespace std; class Budget{ private: static double corpBudget; double divisionBudget; static int objCount; public: Budget(){ divisionBudget = 0; objCount ++; cout << "Budget Obj count = " << objCount << endl ; } void addBudget (double d){ divisionBudget += d; corpBudget += d; } double getBudget() const{ return(divisionBudget); } double getTotalBudget() const { return(corpBudget); } static void mainBudget(double m){ corpBudget += m; } friend void AuxOffice::addBudget(double, Budget &); }; #endif Second File : AuxOffice.h #ifndef AUXILIARY_H #define AUXILIARY_H #include <iostream> using namespace std; class Budget; class AuxOffice { private: double auxillary_budget; static int objCount; public: AuxOffice(){ auxillary_budget = 0; objCount ++; cout << "Auxiliary obj count = " << objCount << endl; } void addBudget(double , Budget &); double getBudget(){ return(auxillary_budget); } }; #endif Third File : AuxOffice.cpp #include "AuxOffice.h" #include "Budget.h" void AuxOffice:: addBudget(double d, Budget &div){ auxillary_budget = d; div.corpBudget += d; } Main File : budget.cpp #include <iostream> #include "AuxOffice.h" #include "Budget.h" using namespace std; int main(){ double div_budget, auxillary_budget, startBudget; cout << "Enter main budget" << endl; cin >> startBudget; Budget::mainBudget(startBudget); Budget div1; Budget div2; Budget div3; AuxOffice aux1, aux2, aux3; cout << "Initial Corp Budget = " << div1.getTotalBudget() << endl; cout << "Enter budget for div1 " << endl; cin >> div_budget; div1.addBudget(div_budget); cout << "Enter Auxiliary budget for aux1 " << endl; cin >> auxillary_budget; aux1.addBudget(auxillary_budget, div1); cout << "Enter budget for div2 " << endl; cin >> div_budget; div2.addBudget(div_budget); cout << "Enter Auxiliary budget for aux2 " << endl; cin >> auxillary_budget; aux2.addBudget(auxillary_budget, div2); cout << "Enter budget for div3 " << endl; cin >> div_budget; div3.addBudget(div_budget); cout << "Enter Auxiliary budget for aux3 " << endl; cin >> auxillary_budget; aux3.addBudget(auxillary_budget, div3); cout << "Budget for Div1 = " << div1.getBudget() << endl; cout << "Budget for Aux1 = " << aux1.getBudget() << endl; cout << "Budget for Div2 = " << div2.getBudget() << endl; cout << "Budget for Aux2 = " << aux2.getBudget() << endl; cout << "Budget for Div3 = " << div3.getBudget() << endl; cout << "Budget for Aux3 = " << aux3.getBudget() << endl; cout << "Total Corp Budget =" << div1.getTotalBudget() << endl; return(1); }

Aucun commentaire:

Enregistrer un commentaire