Issues nesting enum within a struct

I am trying to nest some of my enums within structs so that I can use them in functions involving the other struct variables further down the line. I keep getting a 'name followed by a :: must be a class or namespace' error in the new code for loop. However, that's not an issue in the old code for loop, which also doesn't use classes or namespaces.

Why is the new code throwing up this error? How can I fix it without converting my structs to classes?

oldcode.h

1
2
3
4
const int LONGBUTTON_HEIGHT = 128;
const int LONGBUTTON_WIDTH = 256;

enum CreateForestButtonState { CREATE_DEFAULT, CREATE_HOVER, CREATE_INACTIVE, CREATE_PRESSED, CREATE_TOTAL };


oldcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "oldcode.h"

SDL_Rect create_clips[CreateForestButtonState::CREATE_TOTAL];
for (int i = 0; i < CreateForestButtonState::CREATE_TOTAL; i++)
{
	create_clips[i].x = i * LONGBUTTON_WIDTH;
	create_clips[i].y = 0;
	create_clips[i].w = LONGBUTTON_WIDTH;
	create_clips[i].h = LONGBUTTON_HEIGHT;
}
int useCreate_Clip = CREATE_DEFAULT;


newcode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
const int LONGBUTTON_HEIGHT = 128;
const int LONGBUTTON_WIDTH = 256;

struct Graphic
{
	enum state {DEFAULT, HOVER, INACTIVE, PRESSED, TOTAL};
	int use_clip;
	int x;
	int y;
	int h;
	int w;
};


newcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "newcode.h"

Graphic create_plain;
create_plain.type = "long button";
create_plain.state::DEFAULT;
create_plain.use_clip;

SDL_Rect clips[create_plain.state::TOTAL];
for (int i = 0; i < create_plain.state::TOTAL; i++)
{
	clips[i].x = i * LONGBUTTON_WIDTH;
	clips[i].y = 0;
	clips[i].w = LONGBUTTON_WIDTH;
	clips[i].h = LONGBUTTON_HEIGHT;
}
create_plain.use_clip = create_plain.DEFAULT;
not having SDL, ... this works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

const int LONGBUTTON_HEIGHT = 128;
const int LONGBUTTON_WIDTH = 256;

struct Graphic
{
	enum state {DEFAULT, HOVER, INACTIVE, PRESSED, TOTAL};
	int use_clip;
	int x;
	int y;
	int h;
	int w;
};

int main()
{
Graphic clips[Graphic::TOTAL];
for (int i = 0; i < Graphic::TOTAL; i++)
{
	clips[i].x = i * LONGBUTTON_WIDTH;
	clips[i].y = 0;
	clips[i].w = LONGBUTTON_WIDTH;
	clips[i].h = LONGBUTTON_HEIGHT;
}
clips[0].use_clip = Graphic::DEFAULT;
}


this works too, if you have multiple enums in one:
Graphic clips[Graphic::state::TOTAL]; //yikes, maybe consider a case-style and stick to it?

I do not know if this answered your question? I think you have a syntax error, the .state is wrong, state is not a variable, its a type...

if you want it to be a variable:
enum {DEFAULT, HOVER, INACTIVE, PRESSED, TOTAL} state; //eww, C code!
or better:
enum state_t {DEFAULT, HOVER, INACTIVE, PRESSED, TOTAL}; //whatever style you want.
state_t State;

then you could have this gem:
clips[0].State = Graphic::state_t::PRESSED;

disclaimer -- the language wants you to use the new enum class, but it is a giant hassle because you can't directly to/from int with it, which is half the point of enums in most practical code.
Last edited on
Topic archived. No new replies allowed.