944,189 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2131
  • C++ RSS
Jun 28th, 2005
0

need help overloading =, >>, <<

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 29th, 2005
0

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

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 29th, 2005
0

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

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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 29th, 2005
0

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

C++ Syntax (Toggle Plain Text)
  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;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 29th, 2005
0

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

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

C++ Syntax (Toggle Plain Text)
  1. friend ifstream& operator>>(ifstream& ifs, GamePiece& p); //overloaded ostream operator to input saved board & piece

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 29th, 2005
0

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

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;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 29th, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
M Tritt is offline Offline
20 posts
since Jun 2005
Jun 29th, 2005
0

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

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dynamically Print Out Structure Contents
Next Thread in C++ Forum Timeline: Difference between C#.NET and C++.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC