I'm having some difficulties with friend functions in C++, but my suspicion is that is more of a symptom of a problem I have with preprocessor directives and #include.
This is a silly example of what I was doing. Five files: bobby.h, bobby.cpp, billy.h, billy.cpp, and main.cpp. Billy has a protected function called ReceiveMoney. Bobby has a function called bank that calls Billy's ReceiveMoney. i.e. every time Bobby goes to the bank he splits the money with Billy.
billy.h
#ifndef BILLY_H
#define BILLY_H
#include "bobby.h"
class Billy
{
friend void Bobby::Bank(int, Billy &);
public:
Billy();
protected:
void ReceiveMoney(int inc);
private:
int money;
};
#endif
billy.cpp
#include "billy.h"
Billy::Billy()
{
money = 0;
}
void Billy::ReceiveMoney(int inc)
{
money+=inc;
}
bobby.h
#ifndef BOBBY_H
#define BOBBY_H
#include "billy.h"
class Bobby
{
public:
Bobby();
void Bank(int amount, Billy & b);
protected:
int money;
};
#endif
bobby.cpp
#include "bobby.h"
Bobby::Bobby()
{
money = 0;
}
void Bobby::Bank(int amount, Billy & b)
{
b.ReceiveMoney(amount/2);
}
main.cpp
#include "billy.h"
#include "bobby.h"
int main()
{
Bobby bo;
Billy bi;
bo.Bank(150, bi);
return 0;
}
I get a large number of errors, usually error C2653: 'Bobby' : is not a class or namespace name or error C2653: 'Billy' : is not a class or namespace name
I'm doing this in an empty console project in VS0