Sign in to follow this  

"Friend" parameter

Recommended Posts

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;
			 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; }

			 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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this