class - C++ Friend Function Does Not Have Access To Private Member -



class - C++ Friend Function Does Not Have Access To Private Member -

friend function unable access private fellow member of class in declared

i trying recreate sample programme reading in book demonstrating utilize of friend function. getting error in ever annoying "intellisense" visual studio frankly can't stand, saying defined friend function not have access private fellow member in declaration of friend function exists. code in different files follows:

main.cpp: #include "stdafx.h" #include <iostream> #include "budget.h" using namespace std; int main() { homecoming 0; } budget.h #pragma 1 time #include "auxi.h" class budget { public: budget() { divisionbudget = 0; } double getdivisionbudget() const { homecoming divisionbudget; } void addbudget(int n) { divisionbudget += n; corporatebudget += n; } double getcorporatebudget() { homecoming corporatebudget; } static void setcorporatebudget(double n) { corporatebudget = n; } friend void auxillaryoffice::addbudget(double, budget &); private: static double corporatebudget; double divisionbudget; }; double budget::corporatebudget = 0; auxi.h #pragma 1 time class budget; class auxillaryoffice { public: auxillaryoffice() { divisionbudget = 0; } void addbudget(double n, budget &b); private: double divisionbudget; }; auxi.cpp #include "stdafx.h" #include "auxi.h" #include "budget.h" // class budget; need here? void auxillaryoffice::addbudget(double n, budget &b) { divisionbudget += n; b.corporatebudget += n; // error line }

in above auxi.cpp file, getting error on b.corporatebudget += n; line says corporatebudget inaccessible. legal c++ code or there missing in example?

thanks.

butget.h

#pragma 1 time #include "auxi.h" class budget { public: budget() { divisionbudget = 0; } double getdivisionbudget() const { homecoming divisionbudget; } void addbudget(int n) { divisionbudget += n; corporatebudget += n; } double getcorporatebudget() { homecoming corporatebudget; } static void setcorporatebudget(double n) { corporatebudget = n; } friend void auxillaryoffice::addbudget(double, budget &); private: static double corporatebudget; double divisionbudget; }; double budget::corporatebudget = 0; // here error!!!

you should move double budget::corporatebudget = 0 auxi.cpp.

c++ class oop private friend

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -