class BitmapFile{
public:
friend ifstream& operator>>(ifstream& in, BitmapFile& bmp);
... blah blah
};
//and
#include "BitmapFile.h"
//wont keep the space here!!
ifstream& BitmapFile::operator>>(ifstream& in, BitmapFile& bmp){
blah blah
}
It keeps saying that '>>' is not a member of BitmapFile. I haven't done any operator overloading in quite some time, but I check my books and some old code and I'm not seeing what it is that I'm not doing. I know its something small and retarded, but I've been staring at this for so long that I'm sure it won't just pop out at me..... *sigh*
/emote feels so retarded
/emote strips off his Zealot rank and replaces it with Initiate
Damn that source box is retarded... it i cant make it look right, but you all get the picture!
Edited by - Orpheum on 9/28/00 6:50:09 PM
Wierd error overloading the << and >> operators!
I feel like a damn newbie retard asking this, but I don't have much choice... I am overloading the << and >> operators for my bitmap class (I decided to quit being lazy and actually follow the OOP model). The only problem is that the definition of the overload doesnt take! Given the following:
There is no spoon.
Okay It keeps saying that >> is not a member because it isn''t!
In that source you defined that function to be a ''friend''
You should read up what friend means, it means that this function is Not a member of this class, but it''s allowed to use private data from this type of class.
Either remove ''friend'' from the declaration, or remove ''BitmapFile::'' from the definition.
Just because the church was wrong doesn't mean Galileo wasn't a heritic. It just means he was a heritic who was right.
In that source you defined that function to be a ''friend''
You should read up what friend means, it means that this function is Not a member of this class, but it''s allowed to use private data from this type of class.
Either remove ''friend'' from the declaration, or remove ''BitmapFile::'' from the definition.
Just because the church was wrong doesn't mean Galileo wasn't a heritic. It just means he was a heritic who was right.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
DOH! I was pondering over this for 1/2 hour til you came along and cleared this up! Can't believe I forgot about the friend keyword there. Thanks, Atavist, I was getting frustrated too!
I know how you feel Orpheum... that made me feel like a newbie too.
Edited by - Steel on September 28, 2000 10:52:20 PM
I know how you feel Orpheum... that made me feel like a newbie too.
Edited by - Steel on September 28, 2000 10:52:20 PM
When I remove the friend declaration, I get a compiler error that says >> has too many parameters. I know this is bullshit because I''ve done it before EXACTLY like this. Wtf??!
There is no spoon.
I think you got it backwards Don''t remove the friend spec. remove the class qualifier:
class BitmapFile{public: friend ifstream& operator>>(ifstream& in, BitmapFile& bmp); ... blah blah};//and#include "BitmapFile.h"//wont keep the space here!!ifstream& operator>>(ifstream& in, BitmapFile& bmp){blah blah}
But removing the class qualifier and keeping the friend keyword makes the function NOT a member of the class, and that may not be what Orpheum wants.
I got that same error you got Orpheum. Than I realized that an overloaded operator function that is a member of a class can only take one parameter. There's no need to make two parameters because you can always refer to a the instance to the left of the << operator by using the "this" pointer.
Edited by - Steel on September 29, 2000 7:12:56 PM
I got that same error you got Orpheum. Than I realized that an overloaded operator function that is a member of a class can only take one parameter. There's no need to make two parameters because you can always refer to a the instance to the left of the << operator by using the "this" pointer.
class BitmapFile{public: ifstream operator<<(ifstream& in);};// cannot return a reference to an ifstream because// it will go out of scope as soon as the function// returnsifstream BitmapFile::operator <<(ifstream& in){ ifstream out; // blah, blah // just refer to the current instance with // the this pointer. return out ;}
Edited by - Steel on September 29, 2000 7:12:56 PM
quote:
// cannot return a reference to an ifstream because
// it will go out of scope as soon as the function
// returns
Sure you can, your supposed to pass on the stream object that the function was called with. Returning a copy of a ifstream you create isn't going to be of any use - you don't know where your supposed to output to.
quote:
But removing the class qualifier and keeping the friend keyword makes the function NOT a member of the class, and that may not be what Orpheum wants.
Why would it not be what he wants? If you do make it a member function, you can't use the operator in the way your supposed to, i.e. you can't do:
stream << BitmapFile << SomthingElse;
Edited by - Wilka on September 29, 2000 7:26:28 PM
Thanks to everyone who helped out... I actually got it working a few hours ago. I removed the class specifier, and just tacked out "bmp." to the beginning of every reference to one of BitmapFile''s members. Thanks to Steel for reminding me that overload''s that are part of a class do not need 2 params! We were taught to always use friends to overload so I totally forgot that from my texts (and the POS one I used for referrence didnt even mention it! NOBODY should ever use Micro$oft Visual C++ Language Reference v2.0!!!
There is no spoon.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement