|
"must have an argument of class or enumerated type" |
Kiel ....
Member #4,944
August 2004
|
I'm confused about this particular error the compiler's spewing out at me when I'm trying to overload << like so: class foo{ }; int operator<< (const foo *f, const int &t){ return 0; } int main() { return 0; }
This is the error it gives: So, what exactly does this error mean and is there any way to actually do something like this? Thanks in advance. |
Myrdos
Member #1,772
December 2001
|
It doesn't know what class you're trying to insert to. class foo{ int operator<< (const foo *f, const int &t){ return 0; } }; If you do that, now you'll hear about how it only wants to insert one item at a time. (only one parameter) class foo{ int operator<< (const foo *f){ return 0; } }; Now we're cooking. __________________________________________________ |
Kiel ....
Member #4,944
August 2004
|
That wasn't quite what I had in mind.... Let me explain a bit more. class foo{ int operator<< (const foo *f){ return 0; } }; That'd let you do something like foo f; foo *bar = new foo(); int t = f << bar;
To ultimately set t to 0. foo *bar = new foo(); int t = bar << 1; To ultimately set t to 0 (or whatever the << operator's really doing). In my practical application it'll be insertion into a stream that returns the stream like normal usage. I suppose I could achieve this by just using (*bar) << whatever, but that's a bit more of a hassle. |
Thomas Fjellstrom
Member #476
June 2000
|
Yeah, afaik, you can't define operators for POINTERs. Only class's themselves. A pointer to an object is not an object. Its one of the basic types. -- |
anonymous
Member #8025
November 2006
|
Yeah, that's what the error is saying. Make the overload for your class and dereference the pointer as usual. #include <iostream> class foo{ public: int i; }; int operator<< (const foo& f, int t){ return f.i << t; } int main() { foo* p = new foo; p->i = 37; std::cout << (*p << 1) << '\n'; delete p; }
|
|