0

I am getting an error with a program. Evidently I'm missing something about the syntax. The snippet of C++ code below is the smallest which produces the error.

#include <iostream>
using namespace std;

class Parent
{
    public:
    enum MyEnum {
        Value1,
        Value2,
        Value3
    };

    MyEnum* set;
};

class Child: public Parent
{
    public:
    Child()
    {
      set = new MyEnum[5];
      set[0]=MyEnum.Value1;//<--Something wrong here
      set[1]=MyEnum.Value2;//<--Something wrong here
      set[2]=MyEnum.Value3;//<--Something wrong here
      set[3]=MyEnum.Value2;//<--Something wrong here
      set[4]=MyEnum.Value1;//<--Something wrong here
    }

    void Write()
    {
        for(int i=0;i<5;i++)
        {
            cout<< "This is " << i << ": " << set[i];
        }
    }
};

int main() {
    Child c;
    c.Write();

    return 0;
}

The error has something to do with the indicated syntax.

 expected primary-expression before ‘.’ token

I have tried Parent.MyEnum.Value1, Parent::MyEnum.Value1, etc. nothing seems to be right. How should I be referring to the specific values in the parent class?

BSD
  • 329
  • 3
  • 13
  • `MyEnum.Value1;//<--Something wrong here` - The syntax. It's just `Value1` if you have a regular enum. [Pick a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), it will tell you that and more. – StoryTeller - Unslander Monica Oct 02 '17 at 17:19
  • 1
    What is it about people that they like to assume someone is a novice or ill read because they have a question. Like I don't have three C++ books an arm's reach away. Thanks for that. Getting back to the question, WHY is that syntax acceptable? And what exactly is a "regular enum"? Is there a different kind of enum? – BSD Oct 02 '17 at 17:32
  • 2
    What, your 3 books didn't tell you about scoped and unscoped enumerations? – StoryTeller - Unslander Monica Oct 02 '17 at 17:33
  • Ever try to search for a word you don't know? Like what do you call that thing in the back of your throat. So much book, so little search ability. But thank you for the terminology. – BSD Oct 02 '17 at 17:38
  • 3
    Well, a book may not be searchable, [but the internet is](http://en.cppreference.com/w/cpp/language/enum)(First hit on Google, you can check). You asked why I assume you are a novice? Because you've made a basic syntax error, and didn't search **online** for the correct usage of the construct you misused. That's a novice mistake. So you can save you indignation, my comments were not entirely unwarranted. – StoryTeller - Unslander Monica Oct 02 '17 at 17:40
  • Possible duplicate of [How to use enums in C++](https://stackoverflow.com/questions/12183008/how-to-use-enums-in-c) – Basya Oct 02 '17 at 17:57
  • no, it's not "the smallest which produces the error" – Andriy Tylychko Oct 02 '17 at 18:16
  • @StoryTeller Not searching online cannot be fixed by buying a book. – Daniel H Oct 02 '17 at 18:41
  • @Gruffalo No, but it could easily be the smallest that the OP found which produces the error. This isn’t code golf; a minimal example does not need to be literally minimal. The example is small enough to be easily understood, which is the point. – Daniel H Oct 02 '17 at 18:48
  • @DanielH: this is a good example of how little time OP spent minimising the code, just look at `set[1-4]`. Or looking for many existing answers to similar basic C++ syntax question. – Andriy Tylychko Oct 02 '17 at 21:13

2 Answers2

3

Enums don't require qualification for their values, meaning you should access them like this:

set[0] = Parent::Value1;

If you would like to enforce qualification, you can use strongly typed enums. It looks like this:

enum struct MyEnum {
    Value1,
    Value2,
    Value3
};

set[0] = Parent::MyEnum::Value1;

But then you should print them using an explicit cast, e.g:

cout << static_cast<int>(set[0]) << endl;
Daniel Trugman
  • 8,186
  • 20
  • 41
1

An enum, like a class, defines a scope. A regular enum like you use puts its enumerator names both in its own scope and its containing scope. Since this is scope resolution, not member access, you use :: instead of .. Therefore, you can use Parent::Value1, Value1 (because public and protected names of Parent are visible in Child), or Parent::MyEnum::Value1 or MyEnum::Value1.

If you want to disallow the first or two options, you should use enum class instead of just enum.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Daniel H
  • 7,223
  • 2
  • 26
  • 41
  • Only scoped enumerations define a scope. That's why they were added to the language. For unscoped enumerations `MyEnum::Value1` only works as an extension on MSVC. – StoryTeller - Unslander Monica Oct 02 '17 at 18:57
  • @StoryTeller I haven’t found anything allowing it, but it [works in gcc](https://godbolt.org/g/3jtSkC) and [clang](https://godbolt.org/g/x1QukD) with `-std=c++11 -Wall -Wextra -pedantic`. If you don’t include the `-std` flag, `clang` says it’s from C++11. – Daniel H Oct 02 '17 at 19:24
  • @StoryTeller According to a code comment in the last example [here](http://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration), this works. I haven’t looked at the standard yet, but it matches my what the compilers say so I tentatively believe it. – Daniel H Oct 02 '17 at 19:26
  • Funny fact. It was changed from C++03 to C++11, when only MSVC did indeed support it as an extension. I had my standards confused. Have a +1. – StoryTeller - Unslander Monica Oct 02 '17 at 19:28