Advertisement

Operator overloading.

Started by May 30, 2000 11:15 PM
4 comments, last by Vlion 24 years, 7 months ago
I cant get this code to work.... I`m getting this error: error C2676: binary ''+'' : ''class file'' does not define this operator or a conversion to a type acceptable to the predefined operator What kind of dumbhead typo did i make ? ~V''lion #include class file { private: fstream nfile; char *filename; public: file(char filename[256] = 0); void close(){nfile.close();}; void setioflag(char flag); templatefriend ostream &operator<<(ostream &stream,A data){ nfile << A;return stream; } templatefriend file operator+(B data){ nfile.write((char *) &value, sizeof(B)); } templatefriend file operator+(C data){ nfile.write((char *) &value, sizeof(C)); }; ~file(){nfile.close();}; }; file fio; file::file(char *fn) { filename = fn; nfile.open(fn, ios::app); } void file::setioflag(char flag) { nfile.close(); switch(flag) case ''R'': { { nfile.open(filename, ios::in); break; } case ''B'': { nfile.open(filename, ios::in/ios::out/ios::binary ); break; } case ''W'': { nfile.open(filename, ios::out/ios::binary); break; } case ''T'': { nfile.open(filename, ios::out/ios::in); break; } case ''A'': { nfile.open(filename, ios::app); break; } } } int main() { file FILE ="test.dat"; FILE + 100; FILE.close(); return 0; } I came, I saw, I got programmers block. ~V''lion
~V'lionBugle4d
I suggest you go read a good C++ book
Advertisement
It''s hard to tell what you''re even trying to do there. Without straining too much reading the code, it looks to me like you are trying to add an integer (or a char) to your "file" and you haven''t defined an operator+ to use integers... If you are trying to make a template function, your syntax is wrong. I suggest you go into MSDN, and look up C2676, read up on that error, look up the syntax for templates... blah.

He''s right. Get a book
Um...I don''t really know much about C++ as I am more of a C guy, but I think you need a semi-colon after the closing brace of the class declaration. Like:

class file
{

}; // <<--that semi-colon

I''m not sure on that though and I have no idea when it comes to overloaded operators. Hope this helps, and if not, then I''m sure everyone will get a big laugh out of my ignorance
Maybe the board just mangled your post, but if you want to use friend functions as global operators, you need binary versions, not unary versions. Otherwise it doesn''t know which 2 classes you are trying to add together. Example with template stuff omitted for clarity:
file operator+ (file f, B data){    f.nfile.write((char *) &value, sizeof(B));} 

No idea where that value variable comes from, but I left it there.

And yes, you also need a semi colon after each class, as otherwise the compiler may think you are trying to declare an instance of that class at the same time you define it.

And I''ll add my opinion here: I don''t think using the add operator to append things to a file makes much sense: this is usually a job for the << operator. Or perhaps the += operator: a true + operator for files seems inefficient, if you did it logically. But that''s up to you.
For one thing you left the operator+ overload out of the class definition. I don''t quite understand why you made it a friend of the class. All I see is the operator<<. If you want to use that I sugest that you write code for the operator<< that you specified in the class definition or add the operator+ to the class definition. By the way why are you using templatefriend instead of friend. just curious I have never done it that way.

I hope this helps.

Robert McHugh
Creativity -- Concept -- CodeYour game is nothing if you don't have all three.http://www.wam.umd.edu/~dlg/terc.htm

This topic is closed to new replies.

Advertisement