(const int's) in .h

Here are the code details:

"verbs.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define __VERBS_H__

namespace OVERBS {
	enum OBJ_VERBS { zero, 
	CUDDLE, EMBRACE, FLIP, GROPE, HUG, 
	KISS, LICK, NUDGE, PAT, PINCH, 
	POKE, PULL, RUB, SHAKE, SQUEEZE, 
	TAP, TUG, TURN, WAVE, PEER, 
	PET, CLENCH, CURSE, NUZZLE, SNAP, 
	STROKE, TWIRL, LEAN, GRIP
	, zTOTAL};
}

const int MAX_OBJ_VERBS = (OVERBS::zTOTAL - 1);


"main.h"
1
2
3
4
extern const int MAX_OBJ_VERBS;
struct v_data {
int theData[MAX_OBJ_VERBS];
};


Okay, so I'm getting errors upon compiling like "error C2057: expected constant expression", and, "warning C4200: nonstandard extension used : zero-sized array in struct/union".

Now, what I DON'T want to do is the "#include "verbs.h"" within the main.h file.

How can I get MAX_OBJ_VERBS to be valid in main.h without including verbs.h?
Compilers only compile .cpp files, the .h files are ignored completely. You have to #include the .h files you want compiled in at least one .cpp file. In your case, line 14 of verbs.h should actually be in a .cpp file and not in a .h file.

The way it works is that actual definitions (line 14 of verbs.h) can only exist once, and declarations (line 1 of main.h) can exist any number of times. Since headers are usually included multiple times, you shouldn't put actual definitions in them, only declarations.
Last edited on
Very good point. I never stopped to consider that .h files weren't compiled. That really clears some confusion for me.

My goal is simple. I want to be able to add verbs to the enum list and then be able to compile the program without every single .cpp that has include main.h in it.

I even tried the following:
"test.h"
1
2
#include "verbs.h"
const int MAX_OBJ_VERBS = (OVERBS::zTOTAL - 1);

Then, in "main.h"
 
#include "test.h" 


Then I went and added to the enum and compiled. Sadly, every .cpp file that had included main.h compiled.

Ultimately, it doesn't look like there's any way around it for me. If I didn't have
1
2
3
struct v_data {
int theData[MAX_OBJ_VERBS];
};

sitting in the main.h file, life would be good.
test.h should be test.cpp and should not be #included.

As for your goal, you're completely misunderstanding enumerations and arrays.

The following two segments of code are, 90% of the time, identical:
1
2
3
4
5
6
enum SomeEnum
{
    FirstConst,
    SecondConst = 4,
    ThirdConst,
};
1
2
3
const int FirstConst = 0;
const int SecondConst = 4;
const int ThirdConst = 5;


I don't really think what you're doing will help you achieve your goal.
Last edited on
I'm not misunderstanding enumerations and arrays. I'm using an enum purely as an alternate option.

I fully realize that I could just as well have:
1
2
3
const int a = 1;
const int b = 2;
const int c = 3;

etc. etc...

I don't even mind if that's how it actually is. The goal is to be able to have MAX_OBJ_VERBS change according to how many verbs I add and/or take away.

The code actually used to be like this (in main.h):
1
2
3
4
5
6
7
8
9
10
11
const int CUDDLE = 1;
const int EMBRACE = 2;
const int FLIP = 3;
const int GROPE= 4;
const int HUG =  5;

const int MAX_OBJ_VERBS = 5;

struct v_data {
int theData[MAX_OBJ_VERBS];
};


And life was good. But then from day to day I would always add and/or delete a verb, which in turn MAX_OBJ_VERBS would need to change to.

Now, for the 5 or so .cpp files that needed CUDDLE, EMBRACE, FLIP, GROPE and HUG, and the other 140+ .cpp files that needed v_data.theData[], I was/am trying to be able to add verbs without a 10 minute compile time.

Am I making sense? :)

I see. Here's a simple setup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Verbs.h
#ifndef __Verbs_Header__
#define __Verbs_Header__
namespace Verbs
{
    enum
    {
        Snuggle,
        Accept,
        Flop,
        Grab,
        WhatCreepersJustWant,

        _LastVerb_
    };
    extern const int Count;
}
#endif 
1
2
3
4
5
6
7
//Verbs.cpp
#include "Verbs.h"

namespace Verbs
{
    const int Count = int(_LastVerb_);
}
1
2
3
4
5
6
//other .cpp files
#include "Verbs.h"

///...
int myarray[Verbs::Count];
///... 
Last edited on
Just curious you casted "_LastVerb_" as int() in const int Count = int(_LastVerb_);

Wouldn't const int Count = _LastVerb_; be okay too?

Which is better:
1
2
3
4
5
6
7
namespace OBJ_VERBS {
	enum { zero, 
	CUDDLE, EMBRACE, FLIP, GROPE, HUG,
	, zTOTAL};
	
	const int _MAX_ = int(OBJ_VERBS::zTOTAL - 1);
}

~OR~
1
2
3
4
5
6
7
namespace OBJ_VERBS {
	enum { zero, 
	CUDDLE, EMBRACE, FLIP, GROPE, HUG,
	, zTOTAL};
	
	const int _MAX_ = (OBJ_VERBS::zTOTAL - 1);
}


Neither, because you should remove the "zero" and not subtract 1. The only reason I casted was because, technically, enums are a new type (even though they use int by default).
I need the "zero" in there as a place holder, so, it has a purpose. I personally favor the idea of type casting (OBJ_VERBS::zTOTAL - 1), so I'm leaving it const int _MAX_ = int(OBJ_VERBS::zTOTAL - 1);.

Thanks for all your replies and answers.
Topic archived. No new replies allowed.