CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    error: expected init-declarator...?

    Hello, I am having a little trouble with some syntax.

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

    Code:
    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:

    Code:
    #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:
    Code:
    private:
    		Configuration();
    		Configuration(const Configuration&);
    	
    		~Configuration();
    		
    		// allow this function to create one instance
    		friend Configuration& Config();
    and in configuration.cpp:

    Code:
    Configuration& Config() 
    {
        static Configuration conf;
        return conf;
    }
    What am I doing wrong?

    Thanks,

    Latem
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Thumbs up Re: error: expected init-declarator...?

    Quote Originally Posted by Latem
    What am I doing wrong?
    There are many things that are not correct.

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

    1. Redeclaring an existing member
    2. Trying to change a const value (you have declared x as a const)
    3. Trying to access a static member using "."
    The correct way to access "x" is -
    Code:
    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.

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

  3. #3
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    Re: error: expected init-declarator...?

    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:
    Code:
    CMyDatabaseSingleton::GetInstance ()->InsertRecord ();
    would end up being
    Code:
    Config().InsertRecord();
    But the design of it is the same.

    Thanks,

    Latem
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: error: expected init-declarator...?

    He was trying to initialise the instance of his static, he just wasn't doing it quite right.

    Nesting this in your class:

    Code:
       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:

    Code:
    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;
    };
    Code:
    Configuration::Position::Position()
     : x( 10 ), y( 10 )
    {
    }
    
    const Configuration::Position Configuration::DefaultPosition; // initialises with default
    // constructor of Position, which is 10,10

  5. #5
    Join Date
    May 2005
    Posts
    151

    Re: error: expected init-declarator...?

    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):

    Code:
    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
    };

  6. #6
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    Re: error: expected init-declarator...?

    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:
    Code:
    // 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:
    Code:
    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 [EDIT]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 [/EDIT]

    Latem
    Last edited by Latem; June 6th, 2005 at 11:53 AM.
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: error: expected init-declarator...?

    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:

    Code:
    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
    Code:
    Configuration defaultConfig;
    you will see that defaultConfig automatically has the default configuration here.

  8. #8
    Join Date
    May 2005
    Posts
    151

    Re: error: expected init-declarator...?

    Quote Originally Posted by Latem
    Anyway thanks all for the suggestions. You all get a point [EDIT]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 [/EDIT]

    Latem
    Thanks! I appreciate that.

  9. #9
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    Re: error: expected init-declarator...?

    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:

    Code:
    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
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured