[C++] const enum getter produces error C2208 [SOLVED]

This topic is 4559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all, looks like I'm more rusty then I thought in ol' C++ land. Still usually I'm not so anal about const-ness etc, but I want this project to be esthetically pleasing to me. Anyway I've got another Board class that can get BoardPositions stored away, but I'm making that GetPosition function const, which of course means all functions called on the return var also have to be const. This is by design (I want something to be able to view a state but not be able to change it). I've const'ified [lol] everything else I want to successfully apart from a getter that returns an enum.
#pragma once

class BoardPosition
{
public:
enum PosType
{
EMPTY=0,
WIRE
};

BoardPosition(PosType posType);
~BoardPosition(void);

//Compile error with const: error C2208: 'const int' : no members defined using this type
//Compiles when not const
PosType GetPosType() { return posType; } const;
void SetPosType(PosType posType) { this->posType = posType; };

private:
PosType posType;
};


Any ideas as to why this "error C2208: 'const int' : no members defined using this type" happens? [Edited by - paulecoyote on June 22, 2005 6:35:58 AM]

Share on other sites
You're going to kick yourself. It's nothing to with constness...

just move your declaration of posType in the private section above the definition of the getter.

EDIT: Or at least I think that's it... odd that it works when non-const.

EDIT2: OK i'm completely wrong.

VVV well spotted

Share on other sites
PosType GetPosType() { return posType; } const;

should be:

PosType GetPosType() const { return posType; };

Share on other sites
Thanks guys, props and all that. I think it was one of those "not in the header file" type problems. For reference:

//boardposition.h#pragma once	class BoardPosition	{	public:		enum PosType		{			EMPTY=0,			WIRE		};				BoardPosition(PosType posType);		~BoardPosition(void);		//Compile error with const: error C2208: 'const int' : no members defined using this type		//Compiles when not const		//PosType GetPosType() { return posType; } const;		PosType GetPosType() const;		void SetPosType(PosType posType) { this->posType = posType; };	private:		PosType posType;	};

//boardposition.cpp#include "..\include\boardposition.h"	BoardPosition::BoardPosition(PosType posType)		:posType(posType)	{	}	BoardPosition::~BoardPosition(void)	{	}	BoardPosition::PosType BoardPosition::GetPosType() const	{ 		return posType; 	}

Compiles fine [rolleyes]

rick_appleton> Really? Deitel & Deitel say put the const after. May be it is a style thing.

Share on other sites
Well, I have dozens functions like I gave the signature. Of course not all with enums, but I daresay some are. Have you tried it in the header file with the const between the declaration and the definition (or was it the other way around)?

Share on other sites
Quote:
 Original post by rick_appletonWell, I have dozens functions like I gave the signature. Of course not all with enums, but I daresay some are. Have you tried it in the header file with the const between the declaration and the definition (or was it the other way around)?

Yep you are right - when I did it long hand I ended up putting the const in the right place (before the function body) when declaring the function anyway.

I already rated you up err from a little while ago, so all I can do is give you a

[smile]

So for those following the post, in the Original post the code should have been:

class BoardPosition	{	public:		enum PosType		{			EMPTY=0,			WIRE		};				BoardPosition(PosType posType);		~BoardPosition(void);		//Compile error with const: "error C2208: 'const int' : no members defined using this type" when cost AFTER the function body {}		//Compiles when const in its proper place ( before {...} )		PosType GetPosType() const { return posType; } ;		void SetPosType(PosType posType) { this->posType = posType; };	private:		PosType posType;	};

Share on other sites
Quote:
 Original post by paulecoyote.. so all I can do is give you a [smile]

That's perfectly all right, glad I could help.

Share on other sites
Quote:
Original post by rick_appleton
Quote:
 Original post by paulecoyote.. so all I can do is give you a [smile]

That's perfectly all right, glad I could help.

was appreciated, knew it would have to be something simple. Thing is that compiler error was rubbish...
Quote:
 error C2208: 'const int' : no members defined using this type

... didn't really help.

Share on other sites

This topic is 4559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628706
• Total Posts
2984309

• 23
• 10
• 9
• 13
• 13