Sign in to follow this  
Tradone

Static Help!

Recommended Posts

Tradone    100
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!

Share this post


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

Share this post


Link to post
Share on other sites
Palidine    1315
[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()
{
}



Share this post


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

Share this post


Link to post
Share on other sites
Palidine    1315
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 =)

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