0

Friend function unable to access private member of the class in which it was declared

I am trying to recreate a sample program I am reading in a book demonstrating the use of a friend function. I am getting an error in the ever annoying "Intellisense" with Visual Studio which I frankly can't stand, saying that my defined friend function does not have access to the private member in which the declaration of the friend function exists. The code in different files is as follows:

Main.cpp:

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

int main() {

    return 0;
}

Budget.h

#pragma once
#include "Auxi.h"

class Budget {
public:
    Budget() {
        divisionBudget = 0;
    }

    double getDivisionBudget() const {
        return divisionBudget;
    }

    void addBudget(int n) {
        divisionBudget += n;
        corporateBudget += n;
    }

    double getCorporateBudget() {
        return 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 once

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; Do I need this here?
void AuxillaryOffice::addBudget(double n, Budget &b) {
    divisionBudget += n;
    b.corporateBudget += n; // THIS IS THE ERROR LINE
}

In the immediately above Auxi.cpp file, I am getting an error on the b.corporateBudget += n; line it says that corporateBudget is inaccessible. Is this all legal C++ code or is there something I am missing in this example?

Thanks.

Dominic Farolino
  • 1,362
  • 1
  • 20
  • 40
  • Do you actually get a compile error or is it just IntelliSense complaining? – Brian Bi Apr 07 '15 at 23:17
  • You can declare two classes as friends. I think in your case it is more suitable. – antonpp Apr 07 '15 at 23:31
  • @Brian it is a compiler error complaining about some weird non existent symbols it doesn't know (which aren't there) as well as the intellisense I would like to try however, a function not belonging to a class that is a friend of a class and see if that might work. – Dominic Farolino Apr 07 '15 at 23:41

3 Answers3

1

You are trying to declare a method (AuxillaryOffice::addBudget) as friend of your class (Budget). I believe that it is not possible to declare a single method as friend. I can see two solutions here:

  1. Restructure your program that AuxillaryOffice::addBudget becomes a function (not a class method).
  2. Make class AuxillaryOffice a friend of class Budget.

Please, consider reading this article to get more information about friends.

antonpp
  • 2,333
  • 23
  • 28
1

Butget.h

#pragma once
#include "Auxi.h"

class Budget {
public:
    Budget() {
        divisionBudget = 0;
    }

    double getDivisionBudget() const {
        return divisionBudget;
    }

    void addBudget(int n) {
        divisionBudget += n;
        corporateBudget += n;
    }

    double getCorporateBudget() {
        return 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 to Auxi.cpp.

thinkerou
  • 1,781
  • 5
  • 17
  • 28
  • Ok so it makes sense that when a variable not initialized gets its value called weird things happen, which is why I added that `double Budget::corporateBudget = 0` in the Budget.h file, however why does it not work there and only work in Auxi.cpp? Is it not being called in the.h file and it has to be in an implementation file? – Dominic Farolino Apr 08 '15 at 02:23
  • You are defining the static variable `corporatebudget` in a header file `Budget.h` that means that each *.cpp file that includes this header file defines its own definition. So you should define the static data member in a single *.cpp file. – thinkerou Apr 08 '15 at 03:28
0

Intellisense uses another compiler to check your code, so it can sometimes give different results. The error it gives you here is wrong - your code works.

There is just one error that probably prevents your code from compiling. You define the static corporateBudget in the header. This is a problem as it may be defined multiple times if the header is included multiple times (like you did here).

The cleanest solution would be to create a file Budget.cpp and define it there.

typ1232
  • 5,535
  • 6
  • 35
  • 51