 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Dec 16, 2008 1:31 pm Post subject: Strange enum behaviour |
|
|
Hi all!
I have a question
namespace Scope {
enum Types {Number, Comment};
class Base
{ public:
Types Type;
};
class Comment : public Base
{ public:
string Value;
inline Comment (const string &value)
{
Type = Comment; <-- here g++ gives me an "error: expected
primary-expression before ';' token"
Value = value;
}
};
}
But why?
If I change Type to int, it works fine!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Juan Antonio Zaratiegui V Guest
|
Posted: Tue Dec 16, 2008 6:21 pm Post subject: Re: Strange enum behaviour |
|
|
imixaly4 (AT) gmail (DOT) com escribió:
| Quote: | Hi all!
I have a question
namespace Scope {
enum Types {Number, Comment};
class Base
{ public:
Types Type;
};
class Comment : public Base
{ public:
string Value;
inline Comment (const string &value)
|
This function hides the original Comment (enum)
| Quote: | {
Type = Comment; <-- here g++ gives me an "error: expected
primary-expression before ';' token"
Value = value;
}
};
}
But why?
If I change Type to int, it works fine!
|
You are using your compiler with a lot of warnings off, and scarce ISO
compatibility. It is converting the member function pointer (which
should never have been written that way) to an int. Risky. UB. Sinful.
best regards
Zara
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
mzdude Guest
|
Posted: Tue Dec 16, 2008 6:27 pm Post subject: Re: Strange enum behaviour |
|
|
On Dec 16, 8:31 am, imixa...@gmail.com wrote:
| Quote: | Hi all!
I have a question
namespace Scope {
enum Types {Number, Comment};
class Base
{ public:
Types Type;
};
class Comment : public Base
{ public:
string Value;
inline Comment (const string &value)
{
Type = Comment; <-- here g++ gives me an "error: expected
primary-expression before ';' token"
Value = value;
}
};}
But why?
|
It's because your class is "Comment" and your type is "Comment".
Either fully quality the enum or change the name of the class.
Type = Scope::Comment;
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Chris Uzdavinis Guest
|
Posted: Tue Dec 16, 2008 11:27 pm Post subject: Re: Strange enum behaviour |
|
|
imixa...@gmail.com wrote:
| Quote: | Hi all!
I have a question
namespace Scope {
enum Types {Number, Comment};
class Base
{ public:
Types Type;
};
class Comment : public Base
{ public:
string Value;
inline Comment (const string &value)
{
Type = Comment; <-- here g++ gives me an "error: expected
primary-expression before ';' token"
Value = value;
}
};
}
But why?
If I change Type to int, it works fine!
|
Your code has a lot of "stuff" in it that obscures the real issue.
To simplify a bit, I removed the namespace, the strings, and changed
class to struct, removing access qualifiers. Then to fix the problem,
I changed the assignment to qualify which Comment you meant, since
your code defines that name to have different meanings. One of your
problems is that the class name was hiding the enumerator.
enum Types {Number, Comment};
struct Base
{
Types Type;
};
struct Comment : public Base
{
Comment ()
{
Type = ::Comment; //** Notice the ns qualifier
}
};
I would not recommend you use the same name for a class and for an
enumerator. It's going to be annoying to have to qualify everything,
with little benefit.
Chris
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Dec 16, 2008 11:30 pm Post subject: Re: Strange enum behaviour |
|
|
On Dec 16, 4:31 pm, imixa...@gmail.com wrote:
| Quote: | Hi all!
I have a question
namespace Scope {
enum Types {Number, Comment};
class Base
{ public:
Types Type;
};
class Comment : public Base
{ public:
string Value;
inline Comment (const string &value)
{
Type = Comment; <-- here g++ gives me an "error: expected
primary-expression before ';' token"
Value = value;
}
};}
But why?
|
Too many Comments in your code :)
It looks like the enum value conflicts with class name.
You can try to use "Type = Scope::Comment" statement, it can help you
in Comment class constructor.
But I'm afraid that you'll get a problem with Comment class instances.
int main ()
{
Scope::Comment instance("bla"); // g++ gives "error: expected `;'
before 'instance'"
}
The correct solution is to use another name for the enum value. I
prefer to use prefixes or suffixes, something like follows
enum Types {NumberType, CommentType};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|