Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » "must have an argument of class or enumerated type"

This thread is locked; no one can reply to it. rss feed Print
"must have an argument of class or enumerated type"
Kiel ....
Member #4,944
August 2004
avatar

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:
"error: `int operator<<(const foo*, const int&)' must have an argument of class or enumerated type"

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
avatar

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.
My goal is to reach this usage:

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
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

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;
}

Go to: