Skip to main content
gamedev.net
🔒 Locked

cannot access static member variable from static member function

Started by nlbs 4 replies 900+ views
Original Post
nlbs
nlbs
Here is my Source Code.
class HtmlTagAutoCompleter{
	private:
    static stack<string> tstk;
	public:
    static bool detectStatus(const string& tagName);
};

bool HtmlTagAutoCompleter::detectStatus(const string& tagName){
	if(tstk.empty() || tstk.top() != tagName){
		tstk.push(tagName);
		return TAG_OPENED;
	}else{
		tstk.pop();
		return TAG_CLOSED;
	}
}
When I compile Compiler is firing Linker error.
../cgi++/.libs/libcgixx.so: undefined reference to `HtmlTagAutoCompleter::tstk'
collect2: ld returned 1 exit status
Whats wrong here ??
Promit
Promit
You need to provide a definition for the static member in a source file somewhere -- the header file entry is only a declaration. In other words, you've told the compiler that this member exists, but not where to put it or how to initialize it. So in a source file somewhere, you need:
static stack<string> HtmlTagAutoCompleter::tstk;

Lastly, tstk is a terrible member name, and returning TAG_OPENED or TAG_CLOSED enums from a function that returns bool makes no sense.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
nlbs
nlbs
Sorry I am not able to understand what you are saying.
Can you explain more ??

The Class signature is in a different header file and source is another .cpp file as usual

EDIT

You edited just after I Posted
nlbs
nlbs
Putting
stack<string> HtmlTagAutoCompleter::tstk;
in header solves However did you mean to put it in header or source??

Also which one is better put it in Header or source .cpp ??
Promit
Promit
Put it in the source. If you put it in the header you'll probably end up with multiple redefinition errors.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
trojanman
trojanman
//Headerclass HtmlTagAutoCompleter{	private:    static stack<string> tstk; //declaration	public:    static bool detectStatus(const string& tagName);};


//.cppbool HtmlTagAutoCompleter::detectStatus(const string& tagName){	if(tstk.empty() || tstk.top() != tagName){		tstk.push(tagName);		return TAG_OPENED;	}else{		tstk.pop();		return TAG_CLOSED;	}}//etc...stack<string> HtmlTagAutoCompleter::tstk; //definition

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.