ini12345
July 2nd, 2008, 03:59 PM
Hi,
I'm having some strange compilation errors, this code yields message:
acount.cpp(38) : error C2248: 'InvestmentAccount::InvestmentAccount' : cannot access private member declared in class 'InvestmentAccount'
even though account is a friend class.
it happens with the rest of the account concrete classes as well (family ,twoYearssavings).
I can't get what's wrong here.
I'm adding the relevant part
account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "observer.h"
#include "bank.h"
#include <string>
class AccountImpl;
class Account:public Observer
{
public:
~Account();
static Account* createAccount(Subject* subject, std::string& type, int saving_period, int month, int year);//factory
std::string getAccountType();
int getMonth();
int getYear();
int getSavingPeriod();
void update(Subject* subject)=0;// from bankObserver
void changeImplementaion( std::string& type);//bridge
protected:
Account(Subject* subject);
private:
Subject* getSubject();//get this object's subject (may differ from Bank in the future)
protected:
AccountImpl* acc_impl;// bridge
};
#endif
///////////////////////
account.cpp
#include <ostream>
#include <iostream>
using namespace std;
#include "account.h"
#include "familyAccount.h"
#include "investmentAccount.h"
#include "twoYearsSavingsAccount.h"
#include "accountImpl.h"
Account::Account(Subject* subject)
{
subject->attach(this);
}
Account::~Account()
{
getSubject()->detach(this);
}
Account* createAccount(Subject* subject, std::string& type, int saving_period, int month, int year)
{
if(!((saving_period==3)||(saving_period==7)||(saving_period==10)))
cerr << saving_period << " years: not a legal saving period [3,7,10]\n";
Account* ptrAcc = 0;
if(type == "investment")
ptrAcc = new InvestmentAccount(subject,saving_period,month,year);
else if(type == "two years savings")
ptrAcc = new TwoYearsSavingsAccount(subject,saving_period,month,year);
else if(type == "family")
ptrAcc = new FamilyAccount(subject,saving_period,month,year);
return ptrAcc;
}
/* rest of the code, not relevant (i think)*/
////////////////////////////
familyAccount.h
#ifndef FAMILYACCOUNT_H
#define FAMILYACCOUNT_H
#include <iostream>
#include <string>
#include "account.h"
#include "familyAccountImpl.h"
class FamilyAccount:public Account
{
friend class Account;
public:
~FamilyAccount(){}
void update(Subject* subject)
{
std::cout<<"Family account updated...\n";
}
private:
FamilyAccount( Subject* subject,int saving_period, int month, int year):
Account(subject)
{
acc_impl = new FamilyAccountImpl(subject,saving_period,month,year);
}
};
#endif
I'm having some strange compilation errors, this code yields message:
acount.cpp(38) : error C2248: 'InvestmentAccount::InvestmentAccount' : cannot access private member declared in class 'InvestmentAccount'
even though account is a friend class.
it happens with the rest of the account concrete classes as well (family ,twoYearssavings).
I can't get what's wrong here.
I'm adding the relevant part
account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "observer.h"
#include "bank.h"
#include <string>
class AccountImpl;
class Account:public Observer
{
public:
~Account();
static Account* createAccount(Subject* subject, std::string& type, int saving_period, int month, int year);//factory
std::string getAccountType();
int getMonth();
int getYear();
int getSavingPeriod();
void update(Subject* subject)=0;// from bankObserver
void changeImplementaion( std::string& type);//bridge
protected:
Account(Subject* subject);
private:
Subject* getSubject();//get this object's subject (may differ from Bank in the future)
protected:
AccountImpl* acc_impl;// bridge
};
#endif
///////////////////////
account.cpp
#include <ostream>
#include <iostream>
using namespace std;
#include "account.h"
#include "familyAccount.h"
#include "investmentAccount.h"
#include "twoYearsSavingsAccount.h"
#include "accountImpl.h"
Account::Account(Subject* subject)
{
subject->attach(this);
}
Account::~Account()
{
getSubject()->detach(this);
}
Account* createAccount(Subject* subject, std::string& type, int saving_period, int month, int year)
{
if(!((saving_period==3)||(saving_period==7)||(saving_period==10)))
cerr << saving_period << " years: not a legal saving period [3,7,10]\n";
Account* ptrAcc = 0;
if(type == "investment")
ptrAcc = new InvestmentAccount(subject,saving_period,month,year);
else if(type == "two years savings")
ptrAcc = new TwoYearsSavingsAccount(subject,saving_period,month,year);
else if(type == "family")
ptrAcc = new FamilyAccount(subject,saving_period,month,year);
return ptrAcc;
}
/* rest of the code, not relevant (i think)*/
////////////////////////////
familyAccount.h
#ifndef FAMILYACCOUNT_H
#define FAMILYACCOUNT_H
#include <iostream>
#include <string>
#include "account.h"
#include "familyAccountImpl.h"
class FamilyAccount:public Account
{
friend class Account;
public:
~FamilyAccount(){}
void update(Subject* subject)
{
std::cout<<"Family account updated...\n";
}
private:
FamilyAccount( Subject* subject,int saving_period, int month, int year):
Account(subject)
{
acc_impl = new FamilyAccountImpl(subject,saving_period,month,year);
}
};
#endif