S
Saikrishna
Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.
For example if I take the following code fragement,
assuming this is test.c
/* PROGRAM STARTS HERE...*/
#include <stdio.h>
struct test{
enum
{RED, GREEN, BLUE
}color;
};
void amIColored(struct test * t)
{
if (t->color==RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");
}
int main()
{
struct test t;
t.color= RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */
The above program will work fine if I say
gcc -c test.c
but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.
PS: I still want to keep structure in the above program as it is
without converting into a class.
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.
For example if I take the following code fragement,
assuming this is test.c
/* PROGRAM STARTS HERE...*/
#include <stdio.h>
struct test{
enum
{RED, GREEN, BLUE
}color;
};
void amIColored(struct test * t)
{
if (t->color==RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");
}
int main()
{
struct test t;
t.color= RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */
The above program will work fine if I say
gcc -c test.c
but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.
PS: I still want to keep structure in the above program as it is
without converting into a class.