Dear all:
In C++ I have the following problem. I define a class CA and a struct
B as follows
struct MyStruct
{
CA::TestEnum testEnum;
int a;
}
class CA
{
public:
enum TestEnum{TEST_1, TEST_2};
CA(void){};
~CA(void){};
MyStruct m_strct;
};
The problem is that if I placed above, the compiler cannot know CA in
myStruct is a class and raises an error (since I define the class CA
after structure B).
I have to place
class CA
{
public:
enum TestEnum{TEST_1, TEST_2};
CA(void){};
~CA(void){};
MyStruct m_strct;
};
struct myStruct
{
CA::TestEnum testEnum;
int a;
}
Now, it cannot recognize that MyStruct in class CA is a structure and
raises an error.
Could you know if there is any solution for this issue besides taking
the enum TestEnum{TEST_1, TEST_2} out of the class?
Thanks
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
MBALOVER
|
3/18/2011 6:49:53 AM |
|
On 2011-03-18 13:49, MBALOVER wrote:
>
> Dear all:
>
> In C++ I have the following problem. I define a class CA and a struct
> B as follows
>
>
> struct MyStruct
> {
> CA::TestEnum testEnum;
> int a;
> }
>
>
> class CA
> {
> public:
> enum TestEnum{TEST_1, TEST_2};
> CA(void){};
> ~CA(void){};
> MyStruct m_strct;
> };
>
> The problem is that if I placed above, the compiler cannot know CA in
> myStruct is a class and raises an error (since I define the class CA
> after structure B).
Yep.
> I have to place
>
> class CA
> {
> public:
> enum TestEnum{TEST_1, TEST_2};
> CA(void){};
> ~CA(void){};
> MyStruct m_strct;
> };
>
> struct myStruct
> {
> CA::TestEnum testEnum;
> int a;
> }
>
> Now, it cannot recognize that MyStruct in class CA is a structure and
> raises an error.
Certainly true as well.
> Could you know if there is any solution for this issue besides taking
> the enum TestEnum{TEST_1, TEST_2} out of the class?
Your constraints and requirements are not clear to me. Type TestEnum
must be defined before you create an object of that type. So any
solution that ensures that TestEnum is defined before CA or MyStruct is
defined, solves the problem. E.g. you could
a) define TestEnum within some third class type that is defined before
MyStruct and CA are defined.
b) define TestEnum in namespace scope (you seem to reject this idea, but
don't explain why) before MyStruct and CA are defined.
HTH & Greetings from Bremen
- Daniel
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
ISO
|
3/18/2011 11:24:19 AM
|
|
MBALOVER wrote:
> I define a class CA and a struct B as follows
[slightly shortened and fixed]
> struct MyStruct
> {
> CA::TestEnum testEnum;
> };
>
> class CA
> {
> public:
> enum TestEnum{TEST_1, TEST_2};
> MyStruct m_strct;
> };
>
> The problem is that if I placed above, the compiler cannot know CA in
> myStruct is a class and raises an error (since I define the class CA
> after structure B).
Right.
> class CA
> {
> public:
> enum TestEnum{TEST_1, TEST_2};
> MyStruct m_strct;
> };
>
> struct myStruct
> {
> CA::TestEnum testEnum;
> };
>
> Now, it cannot recognize that MyStruct in class CA is a structure and
> raises an error.
Also true and expected.
> Could you know if there is any solution for this issue besides taking
> the enum TestEnum{TEST_1, TEST_2} out of the class?
You could move 'MyStruct' into the class instead of moving the enumeration
out of it.
> CA(void){};
> ~CA(void){};
BTW, you neither need the semicolons nor the 'void's. ;)
Uli
--
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ulrich
|
3/18/2011 11:26:05 AM
|
|
On Mar 18, 1:49 pm, MBALOVER <mbalov...@gmail.com> wrote:
> Dear all:
>
> In C++ I have the following problem. I define a class CA and a struct
> B as follows
>
> struct MyStruct
> {
> CA::TestEnum testEnum;
> int a;
>
> }
>
> class CA
> {
> public:
> enum TestEnum{TEST_1, TEST_2};
> CA(void){};
> ~CA(void){};
> MyStruct m_strct;
>
> };
>
> The problem is that if I placed above, the compiler cannot know CA in
> myStruct is a class and raises an error (since I define the class CA
> after structure B).
>
> I have to place
>
> class CA
> {
> public:
> enum TestEnum{TEST_1, TEST_2};
> CA(void){};
> ~CA(void){};
> MyStruct m_strct;
>
> };
>
> struct myStruct
> {
> CA::TestEnum testEnum;
> int a;
>
> }
>
> Now, it cannot recognize that MyStruct in class CA is a structure and
> raises an error.
>
> Could you know if there is any solution for this issue besides taking
> the enum TestEnum{TEST_1, TEST_2} out of the class?
Try this (taken out of the class, though):
struct EnumHolder
{
enum EnumTest {v1, v2};
};
class c1 : public EnumHolder
{
EnumTest e_;
};
class c2 : public EnumHolder
{
c1 member_;
EnumTest e_;
};
Code artifact shared between two or more other code artifacts does not
belong in either of them. It's best to place it "above" them (which is
what I did, in a sense). Another "organizational" approach might be to
cluster all in a namespace, so that you still see that they belong
together.
Otherwise, IMO, a public enum rarely "belongs" to a class anyhow. But
only actual usage shows that, so it's not all that bad to change one
you know how it is used, either.
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Goran
|
3/18/2011 5:48:13 PM
|
|
|
3 Replies
272 Views
(page loaded in 0.091 seconds)
|