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

Started by
6 comments, last by paulecoyote 18 years, 9 months ago
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]
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Advertisement
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
PosType GetPosType() { return posType; } const;

should be:

PosType GetPosType() const { return posType; };
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.


Signed, Paul
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
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)?
Quote:Original post by rick_appleton
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)?


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;	};
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote:Original post by paulecoyote
.. so all I can do is give you a
[smile]


That's perfectly all right, glad I could help.
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.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.

This topic is closed to new replies.

Advertisement