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!