enum across files?

I defined

enum symbol {A, B, C};

in file A.h

and in file B.h, I #include "A.h" , but still get error of A , B and C unresolved when using them. WHy? Thanks.
Last edited on
Does A.h include B.h?

If so, you have a circular dependency, which is going to cause weird errors like this.

See this article:
http://www.cplusplus.com/forum/articles/10627/#msg49679
No, A.h don't include B.h......
Well I don't have any other ideas with just the little bit of info you've given.

Can you post some code?
this is my Basic.h
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef BASIC_H_
#define BASIC_H_

#include <iostream>
#include <fstream>
#include <sstream>

#include <vector>
#include <string>

#include <iomanip>
#include <cstdlib>
#include <cmath>

#include <eigen3/Eigen/Dense>

//----------------------------------------------------------------------------------------------------
typedef double numeric_t;
typedef   long   index_t;

//----------------------------------------------------------------------------------------------------
enum direction_e {e, n, w, s, ne, nw, sw, se};
enum    corner_e {NE, NW, SW, SE};
enum neighbour_e {E, N, W, S, EE, NN, WW, SS};

enum PDEBoundaryType_e {nonPDE, Dirichlet, Neumann, Cauchy};
enum    boundaryType_e {interface, inlet, outlet, wall, periodicUpstream, periodicDownstream};

enum  position_t {liquidSide, inside, meniscus, open};
enum  vpStatus_t {none, unknown, found, out, saturated};
enum   sStatus_t {empty, partial, liquid};

enum blockType_e {ghost, flowfield, porousmedia};

enum algorithm_e {SIMPLE, SIMPLER, SIMPLEC, PISO};
enum    scheme_e {upwind, hybrid, QUICK, HayaseQUICK, TVD};

//----------------------------------------------------------------------------------------------------
const int outputPrecision(4);

const index_t voidIndex(-1);

const numeric_t defaultGrowth(1);
const    size_t neighbrNumber(8), nodeNumber(8), sideNumber(4);

const numeric_t pi(3.1415926);
const numeric_t GM(18.02), R(8.3144621);
const numeric_t atm(1.01325e5);

#endif /* BASIC_H_ */ 


and this is my Boundary.h

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef BOUNDARY_H_
#define BOUNDARY_H_

#include "Basic.h"

/*----------------------------------------------------------------------------------------------------
 *
 *                      About Partial Differential Equations' Boundary Conditions
 *
  ----------------------------------------------------------------------------------------------------*/
class PDEBoundary_c {
	std::vector<numeric_t> V;
	PDEBoundaryType_e T;
public:
	PDEBoundary_c() {
		T=nonPDE;
	}

	PDEBoundary_c(const PDEBoundaryType_e & t, const numeric_t & v) {
		T=t;
		if(T==Dirichlet || T==Neumann) {
			V.push_back(v);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	PDEBoundary_c(const PDEBoundaryType_e & t, const numeric_t & val, const numeric_t & der) {
		T=t;
		if(T==Cauchy) {
			V.push_back(val);
			V.push_back(der);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	void setValue(const PDEBoundaryType_e & t, const numeric_t & v) {
		T=t;
		if(T==Dirichlet || T==Neumann) {
			V.push_back(v);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	void setValue(const PDEBoundaryType_e & t, const numeric_t & val, const numeric_t & der) {
		T=t;
		if(T==Cauchy) {
			V.push_back(val);
			V.push_back(der);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	numeric_t value() {
		if(T==Dirichlet || T==Cauchy) {
			return V[0];
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
			return 0;
		}
	}

    numeric_t value(const numeric_t & delta, const numeric_t & theOther, const bool & first) {
		if(T==Neumann) {
			return first?V[0]*delta+theOther:theOther-V[0]*delta;
		}
		else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
			return 0;
		}
    }

	numeric_t derivative() {
		if(T==Cauchy) {
			return V[1];
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
			return 0;
		}
	}
};

/*----------------------------------------------------------------------------------------------------
 *
 *                                        About Boundary Conditions
 *
  ----------------------------------------------------------------------------------------------------*/
class boundary_c {
public:
	PDEBoundary_c UB, VB, PB, CB;
	boundaryType_e T;
	index_t upstream;

	boundary_c() {
		T=inside;
		upstream=voidIndex;
		UB=PDEBoundary_c();
		VB=PDEBoundary_c();
		PB=PDEBoundary_c();
		CB=PDEBoundary_c();
	}

	boundary_c(const boundaryType_e & t) {
		T=t;
		upstream=voidIndex;
		if(T==wall) {
			UB=PDEBoundary_c(Dirichlet, 0);
			VB=PDEBoundary_c(Dirichlet, 0);
			PB=PDEBoundary_c(Neumann, 0);
			CB=PDEBoundary_c(Neumann, 0);
		} else if(T==outlet){
			UB=PDEBoundary_c(Neumann, 0);
			VB=PDEBoundary_c(Neumann, 0);
			PB=PDEBoundary_c(Neumann, 0);
			CB=PDEBoundary_c(Neumann, 0);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	boundary_c(const boundaryType_e & t, const numeric_t & u, const numeric_t & v, const numeric_t & p, const numeric_t & c) {
		T=t;
		upstream=voidIndex;
		if(T==inlet) {
			UB=PDEBoundary_c(Dirichlet, u);
			VB=PDEBoundary_c(Dirichlet, v);
			PB=PDEBoundary_c(Dirichlet, p);
			CB=PDEBoundary_c(Dirichlet, c);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	boundary_c(const boundaryType_e & t, const numeric_t & p) {
		T=t;
		upstream=voidIndex;
		if(T==periodicUpstream) {
			UB=PDEBoundary_c(  Neumann, 0);
			VB=PDEBoundary_c(  Neumann, 0);
			PB=PDEBoundary_c(Dirichlet, p);
			CB=PDEBoundary_c(  Neumann, 0);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	boundary_c(const boundaryType_e & t, const index_t & i, const numeric_t & p, const numeric_t & c) {
		T=t;
		if(T==periodicDownstream) {
			upstream=i;
			UB=PDEBoundary_c(  Neumann, 0);
			VB=PDEBoundary_c(  Neumann, 0);
			PB=PDEBoundary_c(Dirichlet, p);
			CB=PDEBoundary_c(Dirichlet, c);
		} else {
			std::cerr<<"Boundary Type Don't Match!"<<std::endl;
		}
	}

	friend std::istream & operator >> (std::istream & is, boundary_c & b);
	friend std::ostream & operator << (std::ostream & os, boundary_c & b);

};




#endif /* BOUNDARY_H_ */ 


all those PDEBoundaryType_e value cannot be resolved!
Last edited on
I don't see anything really wrong with this code. What is the actual error message you're getting?
symbol "Neumann" cannot be resolved
... " inside"
"nonPDE"
......


All the value defined in enum cannot be resolved
THanks!
symbol "Neumann" cannot be resolved


That's the full error message? Please don't paraphrase. Copy/paste the full message.
Oh, sorry, I think there is something wrong with my IDE, I cannot find the error message in window now, I only see a little bug icon, which shows that when put mouse on it. Maybe after I solve other problems, this will disappear.
Disch! I still cannot get it work!! Don't know the reason! Any enum defined in another file, the type itself can be identified but the value cannot be identified.

For example:

enum symbol_e {A, B, C}; //defined in symbol.h

and then

any

symbol_e theSymbol;//will be ok

but if

theSymbol=B;//will get error message of "B" cannot be resolved.

What the problem??? I am dying!

I don't know what to tell you. That error message makes no sense... I've never seen it before.

Maybe try a small scale program to make sure something isn't horribly wrong?

The below code is legal and compiles just fine:

1
2
3
4
5
6
// header.h

enum Foo
{
    bar, baz
};

1
2
3
4
5
6
7
8
9
10
11
// main.cpp

#include "header.h"

int main()
{
    Foo x;

    x = bar;
    x = baz;
}


If you can't compile that, then there is something horribly wrong with your build tools, and I'd recommend wiping them and reinstalling.


If that works then I'd recommend you try to come up with a simplistic/minimalistic version of your program (just enough to reproduce this behavior) and post that program (in its entirety) here so others can try to compile/debug it.



Barring that, I don't know what else to do here. =/
I think it might mean that you have the same variable enum in two different enums, and the program can't work out how to resolve between the two enums. Prefer instead using enum class and explicitly specify which you prefer, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum class e1 {
    A,
    B,
    C
};

enum class e2 {
    B,
    C,
    D
};

e1 myEnum;
myEnum = e1::B;
myEnum = e2::B; // compiler error
myEnum = B; // compiler error
myEnum = 1; // compiler error 
Thanks all, I create a new project and copied all content to that one, and then it surprisingly work now. but NT3, looks like your method don't work, I will get compiler error saying e1 is not a class or namespace, e2 the same thing.
Last edited on
Hmmm... Well, it worked for me. Maybe your compiler isn't fully C++11 compliant. Here is an example that did work:
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
#include <iostream>

enum class e1 {
    A,
    B,
    C
};

enum class e2 {
    B,
    C,
    D
};

int main() {
    e1 myEnum;
    e2 otherEnum;

    myEnum = e1::B;
    otherEnum = e2::B;

    std::cout << static_cast<int>(myEnum) << '\n';
    std::cout << static_cast<int>(otherEnum) << '\n';

    return 0;
}
1
0
Thanks, NT3, I'll download a newer version and try again! What compiler are you using, by the way?
On windows, I am using MinGW with GCC 4.8.1, though I occasionally use clang. On linux systems I just use gcc 4.8.2. Also, if you are using clang or GCC, you need to specify -std=c++11 to use C++11 features.
Topic archived. No new replies allowed.