What's wrong with my data structures?

I have data structures that bring up a lot of errors. Like the members of them cannot be initialized.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct ScreenAttributes {
	const int SCREEN_WIDTH = 640;
	const int SCREEN_HEIGHT = 480;
	const int SCREEN_BPP = 32;
} ScrAt;

// The surfaces that will be used
struct SCREENS {
	SDL_Surface* message = NULL;
	SDL_Surface* BG = NULL;
	SDL_Surface* screen = NULL;
} Screens;
This is C++11 code, as some compilers rightfully note when restricted to C++98:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
test.cc:4:25: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        const int SCREEN_WIDTH = 640;
                               ^
test.cc:5:26: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        const int SCREEN_HEIGHT = 480;
                                ^
test.cc:6:23: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        const int SCREEN_BPP = 32;
                             ^
test.cc:11:23: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        SDL_Surface* message = NULL;
                             ^
test.cc:12:18: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        SDL_Surface* BG = NULL;
                        ^
test.cc:13:22: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        SDL_Surface* screen = NULL;
                            ^
6 warnings generated.


Why don't you write a constructor?
Last edited on
Cubbi, well I get a lot of errors.

ken@ken-desktop:~/Documents/SRC/SDL$ g++ -o optimized optimizeloadandblit.cpp -lSDL
optimizeloadandblit.cpp:12: error: ISO C++ forbids initialization of member ‘SCREEN_WIDTH’
optimizeloadandblit.cpp:12: error: making ‘SCREEN_WIDTH’ static
optimizeloadandblit.cpp:13: error: ISO C++ forbids initialization of member ‘SCREEN_HEIGHT’
optimizeloadandblit.cpp:13: error: making ‘SCREEN_HEIGHT’ static
optimizeloadandblit.cpp:14: error: ISO C++ forbids initialization of member ‘SCREEN_BPP’
optimizeloadandblit.cpp:14: error: making ‘SCREEN_BPP’ static
optimizeloadandblit.cpp:15: error: uninitialized const ‘ScrAt’
optimizeloadandblit.cpp:19: error: ISO C++ forbids initialization of member ‘message’
optimizeloadandblit.cpp:19: error: making ‘message’ static
optimizeloadandblit.cpp:19: error: invalid in-class initialization of static data member of non-integral type ‘SDL_Surface*’
optimizeloadandblit.cpp:20: error: ISO C++ forbids initialization of member ‘BG’
optimizeloadandblit.cpp:20: error: making ‘BG’ static
optimizeloadandblit.cpp:20: error: invalid in-class initialization of static data member of non-integral type ‘SDL_Surface*’
optimizeloadandblit.cpp:21: error: ISO C++ forbids initialization of member ‘screen’
optimizeloadandblit.cpp:21: error: making ‘screen’ static
optimizeloadandblit.cpp:21: error: invalid in-class initialization of static data member of non-integral type ‘SDL_Surface*’
optimizeloadandblit.cpp: In function ‘int main(int, char**)’:
optimizeloadandblit.cpp:63: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:63: error: ‘const struct ScreenAttributes’ has no member named ‘SCREENBPP’
optimizeloadandblit.cpp:65: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:73: error: ‘struct SCREENS’ has no member named ‘message’
optimizeloadandblit.cpp:74: error: ‘struct SCREENS’ has no member named ‘BG’
optimizeloadandblit.cpp:76: error: ‘struct SCREENS’ has no member named ‘BG’
optimizeloadandblit.cpp:76: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:77: error: ‘struct SCREENS’ has no member named ‘BG’
optimizeloadandblit.cpp:77: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:78: error: ‘struct SCREENS’ has no member named ‘BG’
optimizeloadandblit.cpp:78: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:79: error: ‘struct SCREENS’ has no member named ‘BG’
optimizeloadandblit.cpp:79: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:81: error: ‘struct SCREENS’ has no member named ‘messasge’
optimizeloadandblit.cpp:81: error: ‘struct SCREENS’ has no member named ‘screen’
optimizeloadandblit.cpp:83: error: ‘screen’ was not declared in this scope
optimizeloadandblit.cpp:89: error: ‘struct SCREENS’ has no member named ‘message’
optimizeloadandblit.cpp:90: error: ‘struct SCREENS’ has no member named ‘BG’
If you are going to have a global object like that why not just use a namespace instead?

1
2
3
4
5
6
7
8
9
10
11
namespace ScreenAttributes {
	const int SCREEN_WIDTH = 640;
	const int SCREEN_HEIGHT = 480;
	const int SCREEN_BPP = 32;
}

namespace Screens {
	SDL_Surface* message;
	SDL_Surface* BG;
	SDL_Surface* screen;
}


And acceess them as ScreenAttributes::SCREEN_WIDTH and Screens::message.
Peter87, I was just using data structures for practice... Didn't think this much trouble would come from them.
Topic archived. No new replies allowed.