I'm trying to compile the following class code and keep getting errors on the overload functions. Everything else compiles ok. I've looked at several sources and compared the syntax and I don't see where the problem is. Please help???

header file

#ifndef GAMEPIECE_H
#define GAMEPIECE_H

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <cstddef>

using namespace std;
//********************************************************

class GamePiece
{
public:
	char pName[50];

private:
	int pType;

public:
	GamePiece(); // default constructor
	GamePiece(GamePiece& p); // copy constructor
	~GamePiece(){};

	// the next 4 won't compile in the definition file
	GamePiece &operator=(const GamePiece& right);  
	friend ostream& operator<<(ostream& os, const GamePiece& p);
	friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
	friend ifstream& operator>>(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece

	int operator==(const GamePiece& p) const {return this->pType == p.pType;};  // You must also check to see that the name is the same.too.
	int operator!=(const GamePiece& p) const {return this->pType != p.pType;};  // You must check that type or name is different.

	void PlayerVsPlayer(GamePiece& p1, GamePiece& p2);
	void PlayerVsComputer(GamePiece& p1, GamePiece& c1);
	void ComputerVsComputer(GamePiece& c1, GamePiece& c2);
};

#endif

implementation file

#include "gamepiece.h"

using namespace std;
//********************************************************
GamePiece::GamePiece() // default constructor
:pType(0)
{
	strcpy(pName, NULL);
};

GamePiece::GamePiece(GamePiece& p) // copy constructor
:pType(p.pType)
{
	if(p.pName!=NULL) {
		strcpy (pName, p.pName);
	}
	else {
		strcpy(pName,NULL);
	}
};

//won't compile
GamePiece::GamePiece& operator=(const GamePiece& right)
{
	strcpy(pName, right.pName);
	pType = right.pType;
	return *this;
};

//won't compile
ostream& operator<<(ostream& os, const GamePiece& p)
{
	os << p.pName << endl;
	os << p.pType << endl;
};

//won't compile
ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
{
	ofs << p.pName << endl;
	ofs << p.pType << endl;
};

//won't compile
ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
{
	ifs >> p.pName >> endl;
	ifs >> p.pType >> endl;
};


void GamePiece::PlayerVsPlayer(GamePiece& p1, GamePiece& p2)
{
	cout << "Player 1 ('X'), enter name: ";
	cin >> p1.pName;
	cout << endl;
	cout << "Player 2 ('O'), enter name: ";
	cin >> p2.pName;
	cout << endl << endl;

	p1.pType = 1;//"X"
	p2.pType = 2;//"O"
};

void GamePiece::PlayerVsComputer(GamePiece& p1, GamePiece& c1)
{

	cout << "Player 1 ('X'), enter name: ";
	cin >> p1.pName;
	cout << endl;
	cout << "Player 2 ('O') will be the computer ";
	cout << endl << endl;
	
	strcpy(c1.pName, "Computer");
	p1.pType = 1;
	c1.pType = 2;

};

void GamePiece::ComputerVsComputer(GamePiece& c1, GamePiece& c2)
{
	cout << "Sit back and enjoy the show...";
	cout << endl << endl;

	strcpy(c1.pName, "Computer 1");
	strcpy(c2.pName, "Computer 2");
	c1.pType = 1;
	c2.pType = 2;
};

Operator overloading is very new to me... I understand the concept, but I'm open to any suggestions for the syntax...

Thanks!

GamePiece::GamePiece& operator=(const GamePiece& right)

should be

GamePiece& GamePiece::operator=(const GamePiece& right)

If you return a value, you should return a value.

ostream& operator<<(ostream& os, const GamePiece& p)
{
	os << p.pName << endl;
	os << p.pType << endl;
    return os;
};

And don't end functions with a semicolon.

You can't change a const object.

Sorry I cant say more, but I gotta go.

I made the following changes, but still get errors... what am I missing????

friend ostream& operator<<(ostream& os, const GamePiece& p);
	friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
	friend ifstream& operator>>(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece
ostream& operator<<(ostream& os, const GamePiece& p)
{
	os << p.pName << endl;
	os << p.pType << endl;
	return os;
};
ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
{
	ofs << p.pName << endl;
	ofs << p.pType << endl;
	return ofs;
};

ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
{
	ifs >> p.pName >> endl;
	ifs >> p.pType >> endl;
	return ifs;
};

error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class std::basic_ofstream<char,struct std::char_traits<char> >' (or there is n
o acceptable conversion)

error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class std::basic_ofstream<char,struct std::char_traits<char> >' (or there is n
o acceptable conversion)

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const char [50]' (or there is no acceptable conversion)

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)

#include <fstream>
friend ostream&  operator<< (ostream&  os,  const GamePiece& p);
	friend ofstream& operator<< (ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
	friend ifstream& operator>> (ifstream& ifs, GamePiece& p); //overloaded ostream operator to input saved board & piece
ostream& operator<< (ostream& os, const GamePiece& p)
{
   os << p.pName << endl;
   os << p.pType << endl;
   return os;
}

ofstream& operator<< (ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
{
   ofs << p.pName << endl;
   ofs << p.pType << endl;
   return ofs;
}

ifstream& operator>> (ifstream& ifs, GamePiece& p) //overloaded ostream operator to input saved board & piece
{
   ifs >> p.pName;
   ifs >> p.pType;
   return ifs;
}

I'm sure something should be obvious to me by now, but I'm still getting an error on my ifstream...

friend ifstream& operator>>(ifstream& ifs, GamePiece& p); //overloaded ostream operator to input saved board & piece
ifstream& operator>>(ifstream& ifs, GamePiece& p) //overloaded ostream operator to input saved board & piece
{
	ifs >> p.pName >> endl;
	ifs >> p.pType >> endl;
	return ifs;
}

Don't append the >> endl. I should have highlighted it last time.

ifstream& operator>> (ifstream& ifs, GamePiece& p) //overloaded ostream operator to input saved board & piece
{
   ifs >> p.pName;
   ifs >> p.pType;
   return ifs;
}

I knew it would painfully obvious! Thank you!!!!

One more question regarding overloading... I was reading aobut the -> operator and all I could find was a "smart pointer" class. Is there a way to overload this operator or do you have to include an entire class dedicated to it??

Thanks, again!

Marcia

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.