Archived

This topic is now archived and is closed to further replies.

Error: MyClass& instanceToMyClass

This topic is 5091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I have this problem:
class Singleton<MyClass> g_MyClass; //MyClass is a singleton now

class SomeClass {
public:
	SomeClass() {}
	~SomeClass() {}
private:
	MyClass& m_instance_MyClass;
}
What I wanted was to assign "m_instance_MyClass = g_MyClass::GetInstance();" but I get the compiler error: Eroor: "SomeClass::m_instance_MyClass" must be initialized in constructor base/member initializer list. Just for the sake, I have tried to initialize it in the SomeClass() constructor... but still the same. Do the compiler want me to initialize it in the Singleton... thats absurd? Would be greatful for any help on this, thx!

Share this post


Link to post
Share on other sites
Member variable (edit) references in classes can only be initialized with member initialization lists. So what you need is something like:

SomeClass::SomeClass() : m_instance_MyClass( g_MyClass::GetInstance()) {
// whatever
}


But why the heck are you storing the reference to the singleton anyways?

[edited by - SiCrane on January 6, 2004 9:45:37 AM]

Share this post


Link to post
Share on other sites
Thank you, think I''ll have to read up on the member initialization list

I know I know, heh, I have changed it now... since it''s a singleton, I just use it as a global.

Share this post


Link to post
Share on other sites