[SOLVED]Overloading operator= to take an enum(C++)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

[SOLVED]Overloading operator= to take an enum(C++)

Post by Sanshin77 »

I need some help.
If this is possible to do, it shouldn't be too hard. I want to be able to overload the = operator of my zaColor class to take constants like ZA_RED or ZA_GREEN ( from my ZA_COLOR enum ). Look at the code below, and the example of usage in the end of the post. Some C++ and SDL knowledge is needed to understand the code.

I have a color class:

Code: Select all

class zaColor
{
public:
	zaColor();
	virtual Uint32 GetColor();
	
	zaColor operator=(const ZA_COLOR& color){
		SimpleColor = color;
		return *this;
	}
	ZA_COLOR SimpleColor;
protected:
	bool RGB;
};
a color enum:

Code: Select all

enum ZA_COLOR { ZA_WHITE, ZA_RED, ZA_GREEN, ZA_BLUE, ZA_GREY, ZA_BLACK };
and a drawing function:

Code: Select all

void zaDrawRect(SDL_Rect* rect, zaColor* color = NULL);

void zaDrawRect(SDL_Rect* rect, zaColor* color){
	if(rect){
		if(color)
			SDL_FillRect(pScreen, rect, color->GetColor());
		else{
			SDL_FillRect(pScreen, rect, SDL_MapRGB(pScreen->format, 0xFF, 0xFF, 0xFF));
		}
	}
}


I want to be able to do this in the main function:

Code: Select all

zaColor temp = ZA_RED;
zaDrawRect(200, 50, &temp);
And also, if possible:

Code: Select all

zaDrawRect(200, 50, ZA_RED);
(Note: the zaDrawRect function takes a pointer to a zaColor...)

Thanks for any help that partially or completely solves my problem.

Edit:
If it wasn't clear, the problem is that the operator= function doesn't work.

if I write:
zaColor temp = ZA_RED;
I get the compile error:
"error: conversion from 'ZA_COLOR' to non-scalar type 'zaColor' requested"
Last edited by Sanshin77 on Fri Mar 12, 2010 8:46 pm, edited 2 times in total.
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Overloading operator= to take an enum(C++)

Post by K-Bal »

So what's the matter or did I miss something? ;)
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Re: Overloading operator= to take an enum(C++)

Post by Sanshin77 »

K-Bal wrote:So what's the matter or did I miss something? ;)
hehe, I'm sorry if it wasn't clear. The operator= function doesn't work.

if I write:
zaColor temp = ZA_RED;
I get the compile error:
"error: conversion from 'ZA_COLOR' to non-scalar type 'zaColor' requested"
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Overloading operator= to take an enum(C++)

Post by XianForce »

Uhh, what format is your color in? Is it just a Uint32, or an SDL_Color (Underneath the class of course...)
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Re: Overloading operator= to take an enum(C++)

Post by Sanshin77 »

XianForce wrote:Uhh, what format is your color in? Is it just a Uint32, or an SDL_Color (Underneath the class of course...)
In the class above I just return the color with a switch statement and SDL_MapRGB.

I also have another Color class, zaRGBColor, which uses three Uint8's for the color, but I don't think that has anything to do with the operator problem
Here's the complete zaColor.h file:

Code: Select all

#ifndef __ZA_COLOR_H__
#define __ZA_COLOR_H__

#include "zaCoreLib.h"

class zaColor
{
public:
	zaColor():
	RGB(false),
	SimpleColor(ZA_RED){
		
	}
	
	virtual Uint32 GetColor(){
		switch (SimpleColor) {
			case ZA_WHITE:
				return SDL_MapRGB(pScreen->format, 0xFF, 0xFF, 0xFF);
				break;
			case ZA_RED:
				return SDL_MapRGB(pScreen->format, 0xFF, 0, 0);
				break;
			case ZA_GREEN:
				return SDL_MapRGB(pScreen->format, 0, 0xFF, 0);
				break;
			case ZA_BLUE:
				return SDL_MapRGB(pScreen->format, 0, 0, 0xFF);
				break;
			case ZA_GREY:
				return SDL_MapRGB(pScreen->format, 0x66, 0x66, 0x66);
				break;
			case ZA_BLACK:
				return SDL_MapRGB(pScreen->format, 0, 0, 0);
				break;
		}
	}
	
	zaColor operator=(const ZA_COLOR& color){
		SimpleColor = color;
		return *this;
	}
	ZA_COLOR SimpleColor;
protected:
	bool RGB;
};

class zaRGBColor: public zaColor
{
public:
	zaRGBColor(){
		RGB = true;
	}
	
	Uint32 GetColor(){
		return SDL_MapRGB(pScreen->format, Red, Green, Blue);
	}
	
	Uint8 Red;
	Uint8 Green;
	Uint8 Blue;
private:
	
};

#endif
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Overloading operator= to take an enum(C++)

Post by XianForce »

So SimpleColor is just an int?

if so, you should be able to just overload it with int as the parameter
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Re: Overloading operator= to take an enum(C++)

Post by Sanshin77 »

XianForce wrote:So SimpleColor is just an int?

if so, you should be able to just overload it with int as the parameter
SimpleColor is of type ZA_COLOR, which is an enum:

Code: Select all

enum ZA_COLOR { ZA_WHITE, ZA_RED, ZA_GREEN, ZA_BLUE, ZA_GREY, ZA_BLACK };
This code compiles:

Code: Select all

zaColor operator=(int color){
		SimpleColor = (ZA_COLOR)color;
		return *this;
	}
But I get an error in my main function, when I do this:

Code: Select all

	zaColor temp = ZA_RED;
"Conversion from 'ZA_COLOR' to non-scalar type 'zaColor' requested"


Should either the parameter or the return type be a pointer, const and/or reference?
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Re: Overloading operator= to take an enum(C++)

Post by Sanshin77 »

Well, I eventually found out what to do after experimenting a bit.

The operator= function works like this: " temp = ZA_RED; " but I had to create a separate construct function( zaColor(ZA_COLOR color); ) for it to work with zaColor temp = ZA_RED;
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: [SOLVED]Overloading operator= to take an enum(C++)

Post by K-Bal »

You should not use an enum for this. Define static members for the common colours in your zaColor class.
Post Reply