-2

If create an enum inside a struct for readability, mentioned here How to avoid name conflicts for two enum values with the same name in C++?

I am planning to add more enums, here and in other situations, I just wanted to know why the struct wasn't compiling. Coming from C# and Java I was hoping for a simpler syntax –

And have the struct as the parameter to a constructor in a class, I cannot call it from the main.cpp of a console application. It gives me the error **no matching function for call toBarEnc::BarEnc(BarEnc::Scheme::eScheme)’ ** main.cpp

Here is the Class

class BarEnc {
public:
    struct Scheme
    {
      enum eScheme
      { ADJ1M2, ADJ3M6
      };
    };

    BarEnc();
    BarEnc(BarEnc::Scheme scheme);
}

in main.cpp I call it

BarEnc barEnc = BarEnc(BarEnc::Scheme::ADJ3M6);

But if I change the parameter to an int in the constructor the code compiles

BarEnc(int scheme);

If I change it to the enum the code compiles

BarEnc(BarEnc::Scheme::eScheme scheme);

But when it is a struct, it does not compile. I am relative new to C++, using GCC 4.6 to compile on Linux, using 99 standard.

Is there a reason why I can't use a struct as a parameter?

Community
  • 1
  • 1
pt123
  • 2,146
  • 1
  • 32
  • 57

3 Answers3

2

Simple question: how your enum value should be converted to struct?

Simple answer: there is no way, since there is no suitable constructor.

Your struct has no members, it has only type (enum), so, I have no idea, what you want to do.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • so why are they enclosing enums inside structs in that thread I posted, if you have to write long parameter types like BarEnc::Scheme::eScheme scheme) – pt123 Aug 02 '13 at 11:17
  • @pt123 Because the linked question asked about having several enums that contain the same identifier ("None" in that case), and putting the enums inside structs added a prefix to the names. Since you don't have that problem, the solution is of no use to you. – molbdnilo Aug 02 '13 at 11:39
  • I am planning to add more enums, here and in other situations, I just wanted to know why the struct wasn't compiling. Coming from C# and Java I was hoping for a simpler syntax – pt123 Aug 02 '13 at 12:09
2

When you declare the constructor as

BarEnc(BarEnc::Scheme scheme);

you tell the compiler that the BarEnc constructor takes a structure as argument, and so you can't pass the enumeration value as it's an enumeration and not the structure.


In this case there is really no use for a separate structure just to define the enumeration, you can declare it directly in the surrounding class:

class BarEnc {
public:
    enum eScheme {
        ADJ1M2,
        ADJ3M6
    };

    BarEnc(eShceme scheme);
};

Then when creating BarEnc objects you pass the enumeration value:

BarEnc barenc(BarEnc::eScheme::ADJ1M2);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I thought the point of using structs around enums was to improve readability as you won't confused it with another set of enums – pt123 Aug 02 '13 at 11:23
  • @pt123 Well, if you're afraid of that, just fully qualify the enumeration by writing `BarEnc::eScheme::ADJ1M2` instead, so still no need to put it in an otherwise empty structure. – Some programmer dude Aug 02 '13 at 11:36
0

You can use a struct as parameter. But your struct BarEnc::Scheme in fact has no member.

and the const value BarEnc::Scheme::ADJ3M6 's type is BarEnc::Scheme::eScheme, it cannot auto convert to a struct.

user685684
  • 182
  • 7
  • so why are they enclosing enums inside structs in that thread I posted, if you have to write long parameter types like BarEnc::Scheme::eScheme scheme) – pt123 Aug 02 '13 at 11:20