3

I am self teaching myself in C++ so I just would like to ask for your forgiveness if my question is really basic.

I am following a tutorial on www.learncpp.com

According to the tutorial, I could define my c++ array such as like this

int main()
{
    using namespace std;
    enum ArrayElements
    {
        MAX_ARRAY_SIZE = 5;
    };

    int anArray[MAX_ARRAY_SIZE];
    return 0;
}

But codeblock keep on issuing error

||=== Build: Debug in CH6 (compiler: GNU GCC Compiler) ===|
In function 'int main()':|
|6|error: expected primary-expression before 'enum'|
error: expected ';' before 'enum'|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

I just dont know what is causing the error or is there a problem with the tutorial I am following?

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
Mark Estrada
  • 9,013
  • 37
  • 119
  • 186
  • As I have learned now, declaring it inside the main or outside is not the problem. Its the semicolon in the enum. Perhaps a newbie mistake. Thanks. – Mark Estrada Jul 02 '15 at 08:53
  • 1
    I would recommend a more reliable source that learncpp - there's a good [list of books here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jul 02 '15 at 08:53
  • @molbdnilo Thanks for the link. – Mark Estrada Jul 02 '15 at 09:07

2 Answers2

6

Remove the semicolon inside the enum.

MAX_ARRAY_SIZE = 5;
   //             ^

If you do have more names inside the enum, separate them with a comma ,

enum COLOR
{
    RED,
    BLUE,
    GREEN
};
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • Oh jeez..so its the tutorial that has the problem. Thanks for helping newbies. I don't get the downvote though. Thanks. – Mark Estrada Jul 02 '15 at 08:51
  • @MarkEstrada: post a link to this answer into the tutorial forum: it's a trivial typo, but if fixed will solve many newbie headache! – Emilio Garavaglia Jul 02 '15 at 09:05
-3

replace enum ArrayElements block with following code int MAX_ARRAY_SIZE = 5;

  • So you are suggesting not to follow the tutorial logic and lessons flow and structure, but to follow your one. What should he do with all the other lessons? Do you have a replacement for them all? Just throw everything away just because of a stupid syntax error? – Emilio Garavaglia Jul 02 '15 at 09:36
  • i am surprised with your knee jerk reaction. Can you elaborate where is mistake in my suggestion and where had ever I suggested he should throw all his lessons. – Raj Kumar Jul 15 '15 at 10:52
  • Your answer in fact makes evident you didn't understood the PURPOSE of that lesson. You suggest how to have a working code with minimal effort (in fact suggesting a hack), going in the opposite direction respect to the tutorial plan. – Emilio Garavaglia Jul 15 '15 at 21:07
  • I had honestly tried to offer my suggestion. However I sincerely apologize. – Raj Kumar Jul 16 '15 at 10:42