| | |
need help overloading =, >>, <<
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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
implementation file
Operator overloading is very new to me... I understand the concept, but I'm open to any suggestions for the syntax...
Thanks!
header file
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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)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; };
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
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
I made the following changes, but still get errors... what am I missing????
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)
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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)
C++ Syntax (Toggle Plain Text)
#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 & pieceostream& 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
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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)
friend ifstream& operator>>(ifstream& ifs, GamePiece& p); //overloaded ostream operator to input saved board & piece
C++ Syntax (Toggle Plain Text)
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;
} "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
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Overloading PHP Functions (PHP)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- Overloading matrix bracket operators (C++)
- overloading [] with 2 dimensional arrays (C++)
- need help with simple overloading operator+ for adding two object data. (C++)
- program for finding factorial of a number using *operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: Dynamically Print Out Structure Contents
- Next Thread: Word count
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






