I've never had the need to use the "friend" parameter in any of my object implementations, but alas, you come across the use for something some time or another. So let me break down what I want to be able to do. I have a class, cDescriptor which has a member inside from the class cAccount. I want to be able to access the private members of cAccount from within cDescriptor's methods.
So, this is how I setup my cAccount class:
class cAccount : public iMObject {
friend class cDescriptor;
public:
cAccount(std::string,std::string,std::string,std::list<TiXmlElement*>&,std::list<TiXmlElement*>&);
cAccount(std::string);
virtual ~cAccount(void) { Release( ); }
// i/o operations
static cAccount* LoadAccount(std::string);
bool SaveAccount(void);
void Release(void);
void BuildCharacterList(std::list<TiXmlElement*>&);
void BuildHistoryList(std::list<TiXmlElement*>&);
void NannyMenu(cDescriptor* pDesc,std::string sCommandActive);
void ShowMenu(cDescriptor* pDesc);
std::string GetPassword(void) { return m_sAccountPassword; }
account_char_list GetCharList(void) { return AccountCharList; }
AUTO_SIZE;
private:
bool m_bLoaded;
std::string m_sAccountName;
std::string m_sAccountPassword; // encryption please?
std::string m_sAccountEmail;
account_char_list AccountCharList;
account_history_list AccountHistoryList;
};
I still want to be able to access the public members and methods of cAccount. Everything compiles fine, but when it's time for linking... it seems all the methods are giving me errors (from within the cDescriptor methods). Here's my link errors:
make -s flipmud
o/platform/linux/cDescriptor.o(.text+0x126c): In function `Network::cDescriptor::AccountNanny()':
platform/linux/cDescriptor.cpp:291: undefined reference to `Network::cAccount::LoadAccount(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
o/platform/linux/cDescriptor.o(.text+0x1aa0):platform/linux/cDescriptor.cpp:332: undefined reference to `Network::cAccount::ShowMenu(Network::cDescriptor*)'
o/platform/linux/cDescriptor.o(.text+0x1dac):platform/linux/cDescriptor.cpp:350: undefined reference to `Network::cAccount::cAccount[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
o/platform/linux/cDescriptor.o(.text+0x2d00):platform/linux/cDescriptor.cpp:429: undefined reference to `Network::cAccount::SaveAccount()'
o/platform/linux/cDescriptor.o(.text+0x2eb5):platform/linux/cDescriptor.cpp:445: undefined reference to `Network::cAccount::ShowMenu(Network::cDescriptor*)'
o/platform/linux/cDescriptor.o(.text+0x2ed9):platform/linux/cDescriptor.cpp:447: undefined reference to `Network::cAccount::NannyMenu(Network::cDescriptor*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Everything worked before I needed to add the friend class (I don't NEED to, but I don't want to write Set() methods for each private member). The system I'm compiling on is running Fedora Core 2 with GCC 3.2.