PDA

View Full Version : error: invalid use of void expression



ChineseGeek
2nd October 2009, 05:57
Hi guys,

I have a little problem over here, hope you can help me. When i compile a little application i get this: error: invalid use of void expression

I dont have any idea why im getting that message, the code that gives me problems is the following:


connect(feliz, SIGNAL(clicked()), SLOT(sendImage()));

void SimpleChatClient::sendImage(){
QString html;
QImage img("/home/wae/Qt4/SimpleChat/SimpleChatClient/Iconos/feliz.png");
chat->document()->addResource(QTextDocument::ImageResource, QUrl("/home/wae/Qt4/SimpleChat/SimpleChatClient/Iconos/feliz.png"), img);
html.append("<img src=/home/wae/Qt4/SimpleChat/SimpleChatClient/Iconos/feliz.png>");
socket->write("<" + nick->text().toLatin1() + "> " );
socket->write(chat->setHtml(html));
socket->write("\n");
}

I also added the slot in my .h

Hope you can help me, thank u in advanced!!!

franz
2nd October 2009, 07:13
Could you show the declarations as well?

Lesiok
2nd October 2009, 07:29
connect() needs 4 parameters not 3. You lost a pointer to signal receiver.

wysota
2nd October 2009, 08:15
connect() needs 4 parameters not 3. You lost a pointer to signal receiver.

No, it doesn't. If the receiver is omitted, "this" is assumed.

@ChineseGeek: On which line the compiler complains about?

ChineseGeek
2nd October 2009, 16:20
No, it doesn't. If the receiver is omitted, "this" is assumed.

@ChineseGeek: On which line the compiler complains about?

Hi, the line that gives me the error is:


socket->write(chat->setHtml(html));

Thank u!

ChineseGeek
2nd October 2009, 16:25
Hi, my declaration is this:



QPushButton* feliz = new QPushButton(this);
chat = new QTextEdit(this);


And my .h is:


#ifndef SIMPLECHATCLIENT_H
#define SIMPLECHATCLIENT_H

#include <QWidget>

class QBuffer;
class QSpinBox;
class QLineEdit;
class QTextEdit;
class QTcpSocket;
class QPushButton;

class SimpleChatClient : public QWidget
{
Q_OBJECT

public:
SimpleChatClient(QWidget* parent = 0, Qt::WFlags flags = 0);
~SimpleChatClient();

private slots:
void setConnected();
void setDisconnected();
void toggleConnection();
void sendMessage();
void receiveMessage();
void sendImage();

private:

QBuffer* buffer;
QSpinBox* port;
QLineEdit* nick;
QLineEdit* server;
QLineEdit* message;
QTextEdit* chat;
QTcpSocket* socket;
QPushButton* conn;
QPushButton* send;
QPushButton* feliz;
QPushButton* triste;
QPushButton* bravo;
QPushButton* sorpresa;
QPushButton* llorando;
QPushButton* clock;
QPushButton* confundido;
QPushButton* diablo;
QPushButton* corazon;
QPushButton* balon;
QPushButton* estrella;
QPushButton* enfermo;
};

#endif // SIMPLECHATCLIENT_H

faldzip
2nd October 2009, 17:23
socket->write(chat->setHtml(html));
tell me what do you want to write to socket with that line?
and second question:
what are you already writing to socket with that line?

ChineseGeek
2nd October 2009, 17:27
Hi, i only want to send a .png with a socket and them display that same .png on a QTextEdit.

Thanks for your time!

faldzip
2nd October 2009, 17:31
ok but can you tell me what exactly is that line doing? I know this but I want you to just think for a while and analize that line carefully. What is most important - what exactly you pass to socket to write?

ChineseGeek
2nd October 2009, 17:38
Believe me, i have thought about that line several days hehe... I think im passing a html quote to the socket and them displaying that html quote on the QTextEdit (chat)... Obviously, its wrong, otherwise i wouldnt be asking here hehe...

caduel
2nd October 2009, 17:44
if you don't understand why

socket->write(chat->setHtml(html));
won't work, you need to ask yourself how a void (the result of setHtml) can be written a socket...

The following should compile:

socket->write(chat->setHtml(html)->html());
If that is what you want is another matter.
It is not clear to me (or us, it seems) what you really want to do.

ChineseGeek
2nd October 2009, 17:53
Nop, it didnt work, now this is the error message:

error: void value is not discarded as it should be
error: base operand of '->' is not a pointer

faldzip
2nd October 2009, 17:56
Believe me, i have thought about that line several days hehe... I think im passing a html quote to the socket and them displaying that html quote on the QTextEdit (chat)... Obviously, its wrong, otherwise i wouldnt be asking here hehe...
No, you are passing html string to QTextEdit and result of that method (which is "void" - what you can read in documentation) you are passing to socket. So you are passing void - which is exactly nothing - to the socket. So you are writing nothing. And it is only simple C++ knowledge needed to discover that :P

change it into those lines:


chat->setHtml(html);
socket->write(html);

wysota
2nd October 2009, 19:02
Believe me, i have thought about that line several days hehe...

Oh my God.... :crying:

Let's see... you are loading an image and placing its url into an html document, then setting that html on a text edit with a method returning void and wanting the result of this method to be written to a socket (look in the dictionary what the word "void" means in English) and expecting the contents to be transfered to the other end of the socket... Please tell me I'm wrong... :eek:

ChineseGeek
2nd October 2009, 22:51
@faldzip:

That didnt work neither, error:

error: found no matching function for call ‘QTcpSocket::write(QString&)’

@wysota:

So, what do you recomend to do this? Or better, how can i put that image in the QTextEdit using the socket?

wysota
2nd October 2009, 23:02
So, what do you recomend to do this?
Definitely not a method returning void. And, frankly, surely a good book about C++ and programming in general. Then also a bit of RTFM and STFW.


Or better, how can i put that image in the QTextEdit using the socket?
A good first step would be to browse the reference manual.

ChineseGeek
2nd October 2009, 23:07
Oh come on man! Im a beginner and you are a Guru, you can help me out with this!

wysota
3rd October 2009, 08:59
Im a beginner and you are a Guru, you can help me out with this!

Then act as a beginner and learn the basics first before moving forward to more advanced subjects. If you don't even know what a return type of a method does how do you expect to make progress with using the API?

I still don't know what you want to do so it is quite hard to help you. If you want to transfer this html through the socket then use QTcpSocket::write() as already told. Finding the proper transformation from the type returned by QTextEdit::html() to the type accepted by QTcpSocket::write() is your homework. It should take you no more than 20 seconds to find the appropriate method provided you start looking for it.