2 Replies - 2062 Views - Last Post: 26 November 2012 - 03:34 AM Rate Topic: -----

#1 davidbrent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-November 12

Friend classes in seperate header files

Posted 25 November 2012 - 07:29 PM

I am struggling to enable friendship between two classes in separate header files for a banking program.
I am attempting to get the Person class to use variables from the Account class. Heres what I have so far.



#include <string>
#include <math.h>
#include <windows.h>
#include <vector>
#include "Person.h"

using namespace std;

class person;

class account

int deposit;
friend class person;

void dep(int);	//function to accept amount and add to balance amount
void draw(int);	//function to accept amount and subtract from balance amount
int retdeposit() const;	//function to return balance amount
friend class person;


#ifndef PERSON_H
#define PERSON_H

#include <string>
#include <math.h>
#include <windows.h>

using namespace std;

class person 
	int acno;
	char name[50];
	char add[50];
	char dob[10];
	char type[30];
	char tele[12];
	char mort;
	char mortgage;
	double Principle;
	double initialPrinciple;
	double yearlyInterest;
	double initialYearlyInterest;
	double monthlyInterest;
	double initialMonthlyInterest;
	double monthlyPayment;
	double initialMonthlyPayment;
	int lengthInYears;
	float lengthInYears2;
	double initialLengthinYears;
	int lengthInMonths;
	int initialLengthInMonths;

	void create_account();	//function to get data from user
	void show_account() const;	//function to show data on screen
	void modify();	//function to add new data
	void report() const;	//function to show data in tabular format
	int retacno() const;	//function to return account number
	char rettype() const;	//function to return type of account
	#endif	// PERSON_H
};      //class end

Is This A Good Question/Topic? 0
  • +

Replies To: Friend classes in seperate header files

#2 AKMafia001  Icon User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Friend classes in seperate header files

Posted 26 November 2012 - 03:22 AM

Is it a best friend? ;)
21	friend class person;
27	friend class person;

Well you can read this short tutorial for more information about friend functions and classes...
Was This Post Helpful? 0
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 377
  • View blog
  • Posts: 967
  • Joined: 31-May 11

Re: Friend classes in seperate header files

Posted 26 November 2012 - 03:34 AM

So what is your problem, actually? What were error messages if any?

Some problems that you should look at:
- you don't need or want all those includes in header file. You're not using any type or function from them, so why are they in there in the first place?
- you don't want to have using namespace std; in global scope in header file.
- your #endif from include-guard should be at the end, not in the middle of source file.
- no include-guards!
- again, why all the includes when you don't need any of it?
- pick one - either include Person.h, or use forward declaration of it, why both?
- again using namespace std; in global scope

Neither Person has an object of Account, nor vice-versa. Nor do you even take parameters of these types - so why do you need friendship? Do you use local objects of those in implementation?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1