Skip to main content
GameDev.net gamedev.net
🔒 Locked

Static Help!

Started by Tradone Apr 22, 2006 at 10:16 PM 6 replies 950+ views
Original Post
Tradone
Tradone
Hi, I've read up on static variables for a bit and tried to impliment by static variable but failed. Here's what happeend!!

//Control.cpp
#include "Control.h"

Control::totalControls=0;
Control::skinFileExtension=".shenu";

//Control.h
#if !defined(CONTROL)
#define CONTROL
#include <string>
#include <map>
#include <iostream>
#include "Singleton.h"
#include "Algorithms.h"
#include "Cookie.h"
#include "Parameter.h"
#include "Path.h"
#include "Config.h"
#include "Data.h"
#include "Skin.h"


typedef std::map<std::string, std::map<std::string, std::string>* > DataMap;
typedef Singleton<Algorithms> algorithms;
//header
class Control {
	public:
		Control(Cookie *para_Cookie, Path *para_Path, Parameter *para_Paramter);
		~Control();
		void SetSkinFile(std::string para_skinFile);
		void SetConfig();
		void SetConfig(std::string key, std::string value);
		void SetData();
		void SetDataMap();
		void Display();
		void DisplayCont();
		void DisplayList();
		void RedirectToList();
		void RedirectToManager();
		void Head();
		void Tail();

		static int totalControls;
		const std::string skinFileExtension;
	//end public:

	private:
		std::string skinFile;

		Print controlPrint;
		Cookie *controlCookie;
		Path *controlPath;
		Config *controlConfig;
		Parameter *controlParameter;
		Data *controlData;
		DataMap controlDataMap;
		Skin *controlSkin;


		static const int maxLayersControls=5;
	//end private:
};

#endif



Error: g++ -c -g -O0 Control.cpp -L/usr/local/lib -I/usr/local/include Control.cpp:3: error: expected constructor, destructor, or type conversion before '=' token Control.cpp:3: error: expected ,' or ;' before '=' token Control.cpp:4: error: expected constructor, destructor, or type conversion before '=' token Control.cpp:4: error: expected ,' or ;' before '=' token *** Error code 1 and just wanted to double check that I understand static correctly. So let's say I have a class called loop. and let's say the destructor of loop calls loop itself again. I can count the number of loops with a static int correct? So that when the loop has called itself 5 times, it will stop calling itself. Well, any kind of output, helpful or harmful will be appreciated as always!
dave
dave
You have to put the static keyword before those declarations at the top of the file. It might also be causing you a problem because the static declaration inside the class occurs after external declarations.

I suggest moving the external assignments to the static variables below the class and putting prefix them with static.

I could be talking crap because i should be in bed :)

Hope that helps,

Dave
MaulingMonkey
MaulingMonkey
Quote:
Control::totalControls=0;
Control::skinFileExtension=".shenu";


Try:

<font color="red">int</font> Control::totalControls = 0;<font color="red">std::string</font> Control::skinFileExtension=".shenu";
Palidine
Palidine
[edit: Mauling beat me to it... =( ]

Control::totalControls=0;
Control::skinFileExtension=".shenu";

You're missing the variable type. The pattern you're going for looks like this:

MyClass.h
#ifndef _MYCLASS__#define _MYCLASS__class MyClass{public:    MyClass();private:    static int m_staticOne;    static int m_staticTwo;};#endif //_MYCLASS__


MyClass.cpp
#include "MyClass.h"int MyClass::m_staticOne = 0;int MyClass::m_staticTwo = 0;MyClass::MyClass(){}
MaulingMonkey
MaulingMonkey
Quote:
Original post by Palidine
those need to be initialized _after_ the class decleration. They should _not_ be declared in a header file that will be #include'd elsewhere.


They don't appear to be:

Quote:
//Control.cpp //(not a header)
#include "Control.h" //(class decleration in here)

Control::totalControls=0; //(after the class decleration, included above)
Control::skinFileExtension=".shenu";


Or am I missing something?
Palidine
Palidine
no, you're right Mauling. I didn't notice the line that mentioned it was a header. Used to seeig 2 different source boxes. But i edited my post to remove my mistake =)
MaulingMonkey
MaulingMonkey
Quote:
Original post by Palidine
no, you're right Mauling. I didn't notice the line that mentioned it was a header. Used to seeig 2 different source boxes. But i edited my post to remove my mistake =)


Ahh, righto then :-).
Tradone
Tradone
Thanks for the prompt reply.
Then looks like I fixed this problem. [cool]

[Edited by - Tradone on April 23, 2006 8:07:32 AM]

Topic Locked

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

Sign in to reply to this topic.