Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enum inheritance

Status
Not open for further replies.

livestrong

Technical User
Nov 27, 2006
29
0
0
IE
Hi Everyone,

Im a newbie to c++ and im trying to inherit a enum from class a to class b

Please see below code exerpt.

Code:
#include <iostream>
#include <cstring>

using namespace std;

//class piece
class Piece {
	int rank; //private
	char* row;
	
public:
	//constructor for piece
	enum colour {white, black};

	Piece(int myrank, char* myrow, Piece::colour)
	{
		rank = myrank;
		row = myrow;	
	}
};
class Piece1 : public Piece {
	
public:
	
	Piece1(int myrank, char* myrow, Piece::colour) : Piece(myrank,myrow,Piece::colour)
	{
		//strcpy(name,myname);
	}
};

int main()
{
return 0;
}

Keep getting the below error

Error 1 error C2275: 'Piece::colour' : illegal use of this type as an expression c:\documents and settings\administrator\my documents\visual studio 2005\projects\exercise.cpp 39


Any ideas why?

Thanks
 
1) It doesn't like a reference to the class in the constructor. Change declaration to
Code:
    Piece(int myrank, char* myrow, colour)
2) You need to pass the constuctor something. What you're passing it is just an enum declaration. Change it to something like
Code:
    Piece1(int myrank, char* myrow, Piece::colour [COLOR=red]anycolour[/color]) : Piece(myrank,myrow,[COLOR=red]anycolour[/color])
 
Thats Great thanks, its working now,

Thanks again.
 
Just one more thing about enumerators

How can i compare them

e.g.

Code:
bool Move(int next_rank, char* next_row)
	{
		if (p1::colour = white)
		{
			cout << "Piece white";
		}
		return true;
	}

int main()
{
Piece1 p1("P1",2,"b",Piece::white);
p1.Move(1,"a")
return 0;
}

basically i would like to print the output in the move function.

Any ideas??
 
can do this

if (Piece::white == true)

which is ok

Thanks again
 
1) It should be
Code:
 if (p1.colour == Piece::white)

2) This code is illegal
Code:
if (Piece::white == true)
You are comparing an enum against a bool. OK, granted they may both be 1 but you can't compare things of different types.
 
Yes thanks, again

I realise that now, doh!!

what if i want to put this in a function, how would i refer to p1?
example

Code:
bool next_box(int next_rank, char* next_row)
	{
		
	 if (p1.colour == Piece::white)
         {

           cout << "white"
         }

	return true;
		
	}

I know this code will not work, is there some way to refer to the object in question??

Thanks
again
 
next_box needs to be a method in Piece. Then you don't need to refer to p1. Basically if all the methods live in the class and you create an object of that class, then the methods within the class refer to the object (this->) directly. this-> is implicit but you can put it in if it makes the code any clearer. Also, intellisense will give you a list of options whenever you type this->.
 
ok im not sur i get this 100%

I have defined the below method in Piece class

Code:
bool nextMove(int next_rank, char* next_row)
	{
		if (this->white)
		{
			cout << "white";
		}
		if (this->black)
		{
			cout << "black";
		}
		return true;
		
	}

Also i have the below objects created in main which are of type piece1 and inherits from piece(the father class)

Code:
Piece1 p1("P1",2,"b",Piece::white);
Piece1 p2("P2",3,"z",Piece::black);

So is it possible to find out if P1 is black or white and the same for P2.

My nextMove method just keeps returning "black"


Thanks Again
 
Something like this
Code:
class Piece
{
    
public:
    //constructor for piece
    enum colour {white, black};

    Piece(int myrank, char* myrow, colour mycolour)
    {
        rank = myrank;
        row = myrow;
        m_colour = mycolour
    }

    bool nextMove(int next_rank, char* next_row)
    {
        if (this->m_colour == white)
        {
            cout << "white";
        }
        else if (this->m_colour == black)
        {
            cout << "black";
        }
        return true;
        
    }
private:
    // About the only time that you can use something first
    // and declare it later
    colour m_colour;
    int rank; //private
    char* row;
};
...

Piece1 ...

p1.next_move (...)
 
Thanks xwb,

Think im on track now, learning as i go!

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top