need help overloading =, >>, <<

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 20
Reputation: M Tritt is an unknown quantity at this point 
Solved Threads: 0
M Tritt M Tritt is offline Offline
Newbie Poster

need help overloading =, >>, <<

 
0
  #1
Jun 28th, 2005
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
  1. #ifndef GAMEPIECE_H
  2. #define GAMEPIECE_H
  3.  
  4. #include <iostream>
  5. #include <cctype>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <cstddef>
  9.  
  10. using namespace std;
  11. //********************************************************
  12.  
  13. class GamePiece
  14. {
  15. public:
  16. char pName[50];
  17.  
  18. private:
  19. int pType;
  20.  
  21. public:
  22. GamePiece(); // default constructor
  23. GamePiece(GamePiece& p); // copy constructor
  24. ~GamePiece(){};
  25.  
  26. // the next 4 won't compile in the definition file
  27. GamePiece &operator=(const GamePiece& right);
  28. friend ostream& operator<<(ostream& os, const GamePiece& p);
  29. friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
  30. friend ifstream& operator>>(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece
  31.  
  32. int operator==(const GamePiece& p) const {return this->pType == p.pType;}; // You must also check to see that the name is the same.too.
  33. int operator!=(const GamePiece& p) const {return this->pType != p.pType;}; // You must check that type or name is different.
  34.  
  35. void PlayerVsPlayer(GamePiece& p1, GamePiece& p2);
  36. void PlayerVsComputer(GamePiece& p1, GamePiece& c1);
  37. void ComputerVsComputer(GamePiece& c1, GamePiece& c2);
  38. };
  39.  
  40. #endif

implementation file
  1. #include "gamepiece.h"
  2.  
  3. using namespace std;
  4. //********************************************************
  5. GamePiece::GamePiece() // default constructor
  6. :pType(0)
  7. {
  8. strcpy(pName, NULL);
  9. };
  10.  
  11. GamePiece::GamePiece(GamePiece& p) // copy constructor
  12. :pType(p.pType)
  13. {
  14. if(p.pName!=NULL) {
  15. strcpy (pName, p.pName);
  16. }
  17. else {
  18. strcpy(pName,NULL);
  19. }
  20. };
  21.  
  22. //won't compile
  23. GamePiece::GamePiece& operator=(const GamePiece& right)
  24. {
  25. strcpy(pName, right.pName);
  26. pType = right.pType;
  27. return *this;
  28. };
  29.  
  30. //won't compile
  31. ostream& operator<<(ostream& os, const GamePiece& p)
  32. {
  33. os << p.pName << endl;
  34. os << p.pType << endl;
  35. };
  36.  
  37. //won't compile
  38. ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
  39. {
  40. ofs << p.pName << endl;
  41. ofs << p.pType << endl;
  42. };
  43.  
  44. //won't compile
  45. ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
  46. {
  47. ifs >> p.pName >> endl;
  48. ifs >> p.pType >> endl;
  49. };
  50.  
  51.  
  52. void GamePiece::PlayerVsPlayer(GamePiece& p1, GamePiece& p2)
  53. {
  54. cout << "Player 1 ('X'), enter name: ";
  55. cin >> p1.pName;
  56. cout << endl;
  57. cout << "Player 2 ('O'), enter name: ";
  58. cin >> p2.pName;
  59. cout << endl << endl;
  60.  
  61. p1.pType = 1;//"X"
  62. p2.pType = 2;//"O"
  63. };
  64.  
  65. void GamePiece::PlayerVsComputer(GamePiece& p1, GamePiece& c1)
  66. {
  67.  
  68. cout << "Player 1 ('X'), enter name: ";
  69. cin >> p1.pName;
  70. cout << endl;
  71. cout << "Player 2 ('O') will be the computer ";
  72. cout << endl << endl;
  73.  
  74. strcpy(c1.pName, "Computer");
  75. p1.pType = 1;
  76. c1.pType = 2;
  77.  
  78. };
  79.  
  80. void GamePiece::ComputerVsComputer(GamePiece& c1, GamePiece& c2)
  81. {
  82. cout << "Sit back and enjoy the show...";
  83. cout << endl << endl;
  84.  
  85. strcpy(c1.pName, "Computer 1");
  86. strcpy(c2.pName, "Computer 2");
  87. c1.pType = 1;
  88. c2.pType = 2;
  89. };

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

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help overloading =, >>, <<

 
0
  #2
Jun 29th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 20
Reputation: M Tritt is an unknown quantity at this point 
Solved Threads: 0
M Tritt M Tritt is offline Offline
Newbie Poster

Re: need help overloading =, >>, <<

 
0
  #3
Jun 29th, 2005
I made the following changes, but still get errors... what am I missing????

  1. friend ostream& operator<<(ostream& os, const GamePiece& p);
  2. friend ofstream& operator<<(ofstream& ofs, const GamePiece& p); //overloaded ofstream operator to save board & piece
  3. friend ifstream& operator>>(ifstream& ifs, const GamePiece& p); //overloaded ostream operator to input saved board & piece

  1. ostream& operator<<(ostream& os, const GamePiece& p)
  2. {
  3. os << p.pName << endl;
  4. os << p.pType << endl;
  5. return os;
  6. };
  7. ofstream& operator<<(ofstream& ofs, const GamePiece& p) //overloaded ofstream operator to save board & piece
  8. {
  9. ofs << p.pName << endl;
  10. ofs << p.pType << endl;
  11. return ofs;
  12. };
  13.  
  14. ifstream& operator<<(ifstream& ifs, const GamePiece& p) //overloaded ostream operator to input saved board & piece
  15. {
  16. ifs >> p.pName >> endl;
  17. ifs >> p.pType >> endl;
  18. return ifs;
  19. };

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)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help overloading =, >>, <<

 
0
  #4
Jun 29th, 2005
  1. #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;
}
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 20
Reputation: M Tritt is an unknown quantity at this point 
Solved Threads: 0
M Tritt M Tritt is offline Offline
Newbie Poster

Re: need help overloading =, >>, <<

 
0
  #5
Jun 29th, 2005
I'm sure something should be obvious to me by now, but I'm still getting an error on my ifstream...

  1. friend ifstream& operator>>(ifstream& ifs, GamePiece& p); //overloaded ostream operator to input saved board & piece

  1. ifstream& operator>>(ifstream& ifs, GamePiece& p) //overloaded ostream operator to input saved board & piece
  2. {
  3. ifs >> p.pName >> endl;
  4. ifs >> p.pType >> endl;
  5. return ifs;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help overloading =, >>, <<

 
0
  #6
Jun 29th, 2005
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;
}
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 20
Reputation: M Tritt is an unknown quantity at this point 
Solved Threads: 0
M Tritt M Tritt is offline Offline
Newbie Poster

Re: need help overloading =, >>, <<

 
0
  #7
Jun 29th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help overloading =, >>, <<

 
0
  #8
Jun 29th, 2005
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC