Page 1 of 1

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

Posted: Fri Mar 12, 2010 5:18 pm
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"

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

Posted: Fri Mar 12, 2010 5:39 pm
by K-Bal
So what's the matter or did I miss something? ;)

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

Posted: Fri Mar 12, 2010 5:45 pm
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"

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

Posted: Fri Mar 12, 2010 6:03 pm
by XianForce
Uhh, what format is your color in? Is it just a Uint32, or an SDL_Color (Underneath the class of course...)

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

Posted: Fri Mar 12, 2010 6:09 pm
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

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

Posted: Fri Mar 12, 2010 7:38 pm
by XianForce
So SimpleColor is just an int?

if so, you should be able to just overload it with int as the parameter

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

Posted: Fri Mar 12, 2010 7:55 pm
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?

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

Posted: Fri Mar 12, 2010 8:45 pm
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;

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

Posted: Sat Mar 13, 2010 3:18 am
by K-Bal
You should not use an enum for this. Define static members for the common colours in your zaColor class.