"Friend" parameter

Started by
5 comments, last by bkt 19 years, 8 months ago
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.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Advertisement
bump, I'm still stumped over this.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
cDescriptor needs to include cAccount.h
Have you tried doing this:

class cAccount {
public:
class cDescriptor {

};

friend class cDescriptor;
private:

};

I don't believe what you're doing is legal C++.
Visual Studio .NET 2003 chokes on it.
i think what you need is

class cDescriptor; //class prototype, just like prototyping functions
class cAccount: public iMObject
{
friend class cDescriptor; //im not sure if your supposed to put class in htere or not, though i dont think it matters
.
.
.
};

hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
His code is fine. He is getting the "undefined reference" error because cAccount is not defined in cDescriptor.cpp. Therefore, he needs to include it. Guess that means his code isn't fine.
Actually it is included properly; it worked before I tried using the friend parameter. Still gives me the linker error even when I directly include cAccount in.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]

This topic is closed to new replies.

Advertisement