C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strange enum behaviour

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Dec 16, 2008 1:31 pm    Post subject: Strange enum behaviour Reply with 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!


--
[ 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





PostPosted: Tue Dec 16, 2008 6:21 pm    Post subject: Re: Strange enum behaviour Reply with quote



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





PostPosted: Tue Dec 16, 2008 6:27 pm    Post subject: Re: Strange enum behaviour Reply with quote



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





PostPosted: Tue Dec 16, 2008 11:27 pm    Post subject: Re: Strange enum behaviour Reply with quote

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






PostPosted: Tue Dec 16, 2008 11:30 pm    Post subject: Re: Strange enum behaviour Reply with quote

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
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group