Sign in to follow this  
djwhy

Overloading

Recommended Posts

Hi, I've got a simple 3d engie with a console and I want to write to it like I write to the system console. i.e. I want to do this console << "Some text"; How would I do this. I've had a go at overloading it as if I was overloading it to write to cout or any other ostream but reaplacing the ostream with my console class but I get strange errors when I compile it with gcc: console& console::operator<<(console&, const char*)’ must take exactly one argument What dose this mean? Am I on the right track? [Edited by - djwhy on March 25, 2006 7:08:26 PM]

Share this post


Link to post
Share on other sites
Thanks for the sugestion. I've defined it as a non member function with only the 'const char*' argument but now it says it needs ecactly two arguments. Is this one of those unusual quirks in C++ that isn't very well documented and needs a really really specific solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by djwhy
Thanks for the sugestion. I've defined it as a non member function with only the 'const char*' argument but now it says it needs ecactly two arguments. Is this one of those unusual quirks in C++ that isn't very well documented and needs a really really specific solution.

Lets try again.
Quote:

Define it as a non-member function like this:

console& operator<<(console&, const char*)

Quote:

Or remove the "console&" parameter from the member function.

In other words, choose one of the following:
    non-member function:     console& operator<<(console&, const char*)
member function: console& operator<<(const char*)

Share this post


Link to post
Share on other sites
Why not just inherit your class from std::streambuf? I think that's the preferred method of implementing a new 'stream target' (your engine's console). Then, you can just swap cout's streambuf with your own, and you can use cout to write to the engine console. Or you could inherit your own stream from std::ostream.

note: I could be a bit off - i've never actually tried to do this, but I think this was how it's 'accepted' to be done

Share this post


Link to post
Share on other sites
Thanks dragon, don't think I was getting anywhere with the other way it just didn't work. I tought I would have to do something like that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this