Click to See Complete Forum and Search --> : error: expected init-declarator...?


Latem
June 5th, 2005, 11:08 AM
Hello, I am having a little trouble with some syntax.

I am making a Configuration class.
In configuration.h I have something like this.

class Configuration
{
public:

struct Position
{
int x;
int y;
} Position;

static const struct Default
{
static const struct Position
{
static const int x;
static const int y;
} Position;
} Default;
};

There are lots of configuration variables orginized into structs like so. And then there is the Default struct, that has all the structs, again, and this is used to store the default configuration values.

In my configuration.cpp, I would have:

#include "configuration.h"

/* set all the defaults */
const int Configuration::Default.Position.x = 10;
const int Configuration::Default.Position.y = 10;

//...

Configuration::Configuration()
{
read(); // read the settings or set them to the default values
}
//...

But I keep getting the following errors for the lines in .cpp file where I initialize the default stuff. So each line generates this pair of errors:

error: expected init-declarator before '.' token
error: expected `,' or `;' before '.' token

If its of any relevance, The Configuration class has a private constructor,a nd private copy constructor, and is accessed using a friend function.

so in configuration.h:
private:
Configuration();
Configuration(const Configuration&);

~Configuration();

// allow this function to create one instance
friend Configuration& Config();

and in configuration.cpp:

Configuration& Config()
{
static Configuration conf;
return conf;
}

What am I doing wrong?

Thanks,

Latem

Siddhartha
June 5th, 2005, 11:26 AM
What am I doing wrong?There are many things that are not correct.

This -
const int Configuration::Default.Position.x = 10; is wrong as you are:


Redeclaring an existing member
Trying to change a const value (you have declared x as a const)
Trying to access a static member using "."
The correct way to access "x" is -
Configuration::Default::Position::x I am not assigning it as it is a const.

Remember that to use a static member, one must define it. This has also not been done.

I presume that the objective of your code is to have one place where you can keep data and guard it against duplication, right?

Then, you must consider using the Singleton Design Pattern for the same. Here you can read How to make a Singleton class (http://www.codeguru.com/forum/showthread.php?p=1144723#post1144723).

Singletons will ensure that only one instance of your class exists, and you don't have to use this seemingly complicated method... :thumb:

Latem
June 5th, 2005, 01:40 PM
Not thinking, and just doing copy and paste from my other parts of code is gonna be the end of me...
anyway got it all straigtened out. thanks.

As far as the design of it goes; maybe this isn't evidant from the code and info I supplied, but I am using singleton design Only one Configuration object is ever created, the first time Config() function is called. It's just instead of having a static pointer member, and a static member method that returns it; I have a friend function that returns the static reference. The way I do it, compared to the example in your post:
CMyDatabaseSingleton::GetInstance ()->InsertRecord ();
would end up being
Config().InsertRecord();

But the design of it is the same.

Thanks,

Latem

NMTop40
June 5th, 2005, 04:29 PM
He was trying to initialise the instance of his static, he just wasn't doing it quite right.

Nesting this in your class:


static const struct Default
{
static const struct Position
{
static const int x;
static const int y;
} Position;
} Default;


I'm not sure it's even legal but I think it is. But it's not really good to use the same name for a type as an instance of that type. Can lead to ambiguities later on.

You're overusing static. Instead you can get away with using it just once. And as Default has only member of type Position in it, how about:


class Configuration
{
public:
struct Position
{
int x;
int y;
Position( int x0, int y0 ) : x( x0 ), y( y0 );
Position();
};

Position m_curPosition;
static const Position DefaultPosition;
};


Configuration::Position::Position()
: x( 10 ), y( 10 )
{
}

const Configuration::Position Configuration::DefaultPosition; // initialises with default
// constructor of Position, which is 10,10

microcode
June 6th, 2005, 11:01 AM
I agree that 'static' is not needed on the nested structures, but I don't think the Default structure is needed at all. An alternative way to define it is like this (I included a Size structure just to show that more structures could be added to the Configuration class):


class Configuration
{
public:
struct Position
{
int x;
int y;
};

struct Size
{
int w;
int h;
};

Position position; // Current position
Size size; // Current size

static const Configuration defaultConfig; // Default configuration
};

const Configuration Configuration::defaultConfig =
{
{ 20, 30 }, // Default position
{ 100, 200 }, // Default size
};

Latem
June 6th, 2005, 11:51 AM
Yea I did go a little paranoid with static. But after loking at what I wanted again, actaully I did end up using almost exactly what you suggested microcode. I just opted to using a struct instead of a member variable of type Configuration.

for example a minimal version of the configuration class:
// this is the position settings
struct PositionStruct
{
int X;
int Y;
};

// this is the size settings
struct SizeStruct
{
int W;
int H;
};

// these are all the default settings
struct DefaultStruct
{
const PositionStruct Position;
const SizeStruct Size;
};

class Configuration
{
public:
void read();
void write() const;

PositionStruct Position;

SizeStruct Size;

static const DefaultStruct Default;


private:
Configuration();
Configuration(const Configuration&);


// allow this function to create one instance
friend Configuration& Config();
};


and in the cpp:

const DefaultStruct Configuration::Default =
{
{ 10, 10 }, // Default position
{ 10, 10 }, // Default size
};

I just thought it made more logical sense, and looked nicer with a DefaultStruct. To me anyways. But Configuration member makes sense as well. I didnt want to go creating constructors for all the structs, cuz I'll have like 5, or 6 of them. and it just adds more code in the cpp file, that is not really needed, since I wont use or create those structs anywhere else, and all i wanted was an easy and quick way of initializng the default part of the configuration object.

Anyway thanks all for the suggestions. You all get a point :) Micro you'll have to wait I guess, cuz appearantly i've been too generous, and given out too many points within the last 24 hrs. lol :)

Latem

NMTop40
June 6th, 2005, 06:23 PM
Given this is C++ you really should use constructors to initialise classes, which include structs (which are simply classes with default public access).

Do you need a DefaultConfig?

It might be useful for optimisation if you are constructing this particular default a lot, but otherwise you could just use default values in your constructor. Thus:


class Configuration
{
public:
enum { defaultX=20, defaultY=30, defaultWidth=100, defaultHeight=200 };
struct Position
{
int x;
int y;
Position( int x0=defaultX, int y0=defaultY ): x( x0 ), y(y0) {}
};

struct Size
{
int w;
int h;
Size( int w0=defaultWidth, int h0=defaultHeight): h(h0), y(y0) {}
};

Position position; // Current position
Size size; // Current size

Configuration(int x=defaultX, int y=defaultY, int w=defaultWidth,
int h=defaultHeight ) : position( x,y ), size( w,h )
{
}
};

Of course that is overdoing it a bit - I didn't need to declare the defaults in 3 different constructors - one would probably do.

If you simply call

Configuration defaultConfig;

you will see that defaultConfig automatically has the default configuration here.

microcode
June 6th, 2005, 06:54 PM
Anyway thanks all for the suggestions. You all get a point :) Micro you'll have to wait I guess, cuz appearantly i've been too generous, and given out too many points within the last 24 hrs. lol :)

Latem
Thanks! I appreciate that.

Latem
June 7th, 2005, 09:58 AM
Do you need a DefaultConfig?

Yes. I know maybe it's not evident from the code I provided, but the default stuff isn't suppose to be used to directly set all the settings in the configuration object to the diffault values.

For example the Configuration class has a very simple constructor:

Configuration::Configuration()
{
read(); // read the settings or set them to the default values
}

and the read method reads all the settings from the configuration file. Here if, for whatever reason, a setting could not be read successfully then it is initialized to the default value.

Also the configuration dialog has a default button, that when pressed, all the settings controls in the dialog will be set to the default values. However, the configuration object should not be changed though, until the user presses the OK button. So I actually need all the default values at all time somewhere. Thats why it's static const.

Really, I just wanted to use structs to group my variables into some logical grouping inside the class; and not have a big list of variables. As for using constructors for the structs, i agree its probably more proper, but, my purpoce was just for the grouping, and they wont be used anywhere else. So i just wanted some quick and ez way to initialize the default part.

Thanks,

Latem