1

I'm learning enumerations. I have the following problem: I don't understand, why this works:

enum {fahrrad = 1, Auto = 2} einFahrzeug;
int main() {
    einFahrzeug = fahrrad;

but this doesn't:

enum {fahrrad = 1, Auto = 2} einFahrzeug;
einFahrzeug = fahrrad;

int main() { ...

I would be very happy about an answer

JimmyDiJim
  • 61
  • 5
  • 3
    This doesn't have anything to do with `enum`s. You can't have executable code in global scope. Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Apr 29 '18 at 19:09

1 Answers1

1

You can't assign stuff to variables outside of a function. More simple example:

#include <iostream>

int x;
x = 42;

int main()
{
    std::cout << x << '\n';
}

This gives:

prog.cpp:4:1: error: ‘x’ does not name a type
 x = 42;
 ^

Try it with ideone.com: https://ideone.com/A1K06A

eesiraed
  • 4,626
  • 4
  • 16
  • 34