S
Stefano Sabatini
Hi guys,
the following simple C program is compiled with no problems:
#include <stdlib.h>
#include <stdio.h>
enum ColorId { green, blue, red, yellow, colorNb };
typedef struct Color {
enum ColorId id;
char *str;
} Color;
static Color Colors[] = {
[green] = { green, "green"},
[blue] = { blue, "blue"},
[red] = { red, "red" },
[yellow] = { yellow, "yellow"}
};
int main() {
int i;
printf ("%d\n", colorNb);
for (i=0; i< colorNb; i++)
printf("%s\n", Colors.str);
return 0;
}
It creates a static table Colors containing a limited number of Color
objects, which are then accessed through array subscripting using the
enum values.
I would like to implement a similiar thing in C++, and the nearest
thing I could achieve is this:
#include <iostream>
#include <string>
using namespace std;
enum ColorId { green, blue, red, yellow, colorNb };
class Color {
ColorId id;
string str;
Color(ColorId _id, string _str) { id= _id; str= _str; }
};
static Color Colors[] = {
[green] = { green, "green"},
[blue] = { blue, "blue"},
[red] = { red, "red" },
[yellow] = { yellow, "yellow"}
};
ostream &operator<<(ostream &str, const Color& color) {
string type;
str << color.str;
}
int main() {
for (int i=0; i< colorNb; i++)
cout << Colors << endl;
return 0;
}
but I'm getting these errors:
g++ -I/home/stefano/include/ Colors.cpp -o Colors
Colors.cpp:18: error: expected primary-expression before ‘[’ token
Colors.cpp:18: error: expected primary-expression before ‘{’ token
Colors.cpp:18: error: expected `}' before ‘{’ token
Colors.cpp:18: error: expected ‘,’ or ‘;’ before ‘{’ token
Colors.cpp:18: error: expected unqualified-id before ‘,’ token
Colors.cpp:19: error: expected unqualified-id before ‘[’ token
Colors.cpp:19: error: expected unqualified-id before ‘,’ token
Colors.cpp:20: error: expected unqualified-id before ‘[’ token
Colors.cpp:20: error: expected unqualified-id before ‘,’ token
Colors.cpp:21: error: expected unqualified-id before ‘[’ token
Colors.cpp:22: error: expected declaration before ‘}’ token
make: *** [Colors] Error 1
It seems the [enum val] = notation isn't unrecognized.
So my question is: how can I initialize in C++ a static table putting
object in a predefined position?
Is the above requirement (the creation of a static table containing
all the colors definition, that is containing all the definitions of
the Color object which will be used in the program) a meaningful one
or there are better mechanisms to achieve the same result in C++?
Many thanks in advance for any insight.
Regards.
the following simple C program is compiled with no problems:
#include <stdlib.h>
#include <stdio.h>
enum ColorId { green, blue, red, yellow, colorNb };
typedef struct Color {
enum ColorId id;
char *str;
} Color;
static Color Colors[] = {
[green] = { green, "green"},
[blue] = { blue, "blue"},
[red] = { red, "red" },
[yellow] = { yellow, "yellow"}
};
int main() {
int i;
printf ("%d\n", colorNb);
for (i=0; i< colorNb; i++)
printf("%s\n", Colors.str);
return 0;
}
It creates a static table Colors containing a limited number of Color
objects, which are then accessed through array subscripting using the
enum values.
I would like to implement a similiar thing in C++, and the nearest
thing I could achieve is this:
#include <iostream>
#include <string>
using namespace std;
enum ColorId { green, blue, red, yellow, colorNb };
class Color {
ColorId id;
string str;
Color(ColorId _id, string _str) { id= _id; str= _str; }
};
static Color Colors[] = {
[green] = { green, "green"},
[blue] = { blue, "blue"},
[red] = { red, "red" },
[yellow] = { yellow, "yellow"}
};
ostream &operator<<(ostream &str, const Color& color) {
string type;
str << color.str;
}
int main() {
for (int i=0; i< colorNb; i++)
cout << Colors << endl;
return 0;
}
but I'm getting these errors:
g++ -I/home/stefano/include/ Colors.cpp -o Colors
Colors.cpp:18: error: expected primary-expression before ‘[’ token
Colors.cpp:18: error: expected primary-expression before ‘{’ token
Colors.cpp:18: error: expected `}' before ‘{’ token
Colors.cpp:18: error: expected ‘,’ or ‘;’ before ‘{’ token
Colors.cpp:18: error: expected unqualified-id before ‘,’ token
Colors.cpp:19: error: expected unqualified-id before ‘[’ token
Colors.cpp:19: error: expected unqualified-id before ‘,’ token
Colors.cpp:20: error: expected unqualified-id before ‘[’ token
Colors.cpp:20: error: expected unqualified-id before ‘,’ token
Colors.cpp:21: error: expected unqualified-id before ‘[’ token
Colors.cpp:22: error: expected declaration before ‘}’ token
make: *** [Colors] Error 1
It seems the [enum val] = notation isn't unrecognized.
So my question is: how can I initialize in C++ a static table putting
object in a predefined position?
Is the above requirement (the creation of a static table containing
all the colors definition, that is containing all the definitions of
the Color object which will be used in the program) a meaningful one
or there are better mechanisms to achieve the same result in C++?
Many thanks in advance for any insight.
Regards.