CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: friend class compilation error

  1. #1
    Join Date
    Jun 2008
    Posts
    10

    friend class compilation error

    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
    Code:
    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
    Last edited by ini12345; July 2nd, 2008 at 05:00 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Richmond, BC
    Posts
    79

    Re: friend class compilation error

    There is a mistake in account.cpp:
    Code:
    Account* createAccount(Subject* subject, std::string& type, int saving_period, int month,  int year)
    should be
    Code:
    Account* Account::createAccount(Subject* subject, 
    std::string& type, int saving_period, int month,  int year)

  3. #3
    Join Date
    Jun 2008
    Posts
    10

    Re: friend class compilation error

    my bad.
    I have been struggling to understand what's wrong and it was right in front of me...
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)