PDA

View Full Version : operator<< QString



kiker99
4th May 2006, 21:56
Hello,

i'm sure someone had the same problem before. I am trying to make one of my classes able to receive "<<"-Operations. this is my code:



// messagepipe.h
friend void operator<<(MessagePipe *pipe, QString string);

//messagepipe.cpp
void operator<<(MessagePipe *pipe, QString string)
{
//do something
}


MessagePipe is my class. This produces an error if used like this:



MessagePipe *hint;
hint << "Hi there.";


--> error: invalid operands of types 'MessagePipe*' and 'const char [10]' to binary 'operator<<'

Hm, he isn't able to get a QString out of "Hi there". This seems to work only if used as a real parameter. Does anybody understand this behaviour? I don't want to write everytime QString("My Text"). Any ideas? Thanks.

wysota
4th May 2006, 21:57
Try "const QString &" instead of QString.

kiker99
4th May 2006, 22:02
Try "const QString &" instead of QString.

thanks for the fast reply, but this doesn't change anything here :(
Is this supposed to help or are you guessing?

shad
4th May 2006, 22:14
'const QString&' should help.
my original posting was incorrect

kiker99
4th May 2006, 22:34
'const QString&' should help.
my original posting was incorrect

code looks like this now.


// messagepipe.h
friend void operator<<(MessagePipe *pipe, const QString& string);

// messagepipe.cpp
void operator<<(MessagePipe *pipe, const QString& string)
{
pipe->message(string);
}


the error is still exactly the same. :(
I even recompiled the project, but that didn't help either.

orb9
4th May 2006, 23:38
Make the pointer a reference and it should work. You could also return the pipe to allow chaining.


// messagepipe.h
friend MessagePipe& operator<<(MessagePipe &pipe, const QString& string);

// messagepipe.cpp
MessagePipe& operator<<(MessagePipe &pipe, const QString& string)
{
pipe.message(string);
return pipe;
}

kiker99
5th May 2006, 00:23
yihaa, thx a lot @orb9. I had to change everything for references, but now it is working :D

to tired to think about the reason for this...