Q
Q
QtRoS2015-01-10 23:09:20
Qt
QtRoS, 2015-01-10 23:09:20

C++, the compiler is behaving strangely or am I missing something?

I can't figure out why this code throws an error:

QNetworkRequest request();
request.setRawHeader("", "");

error: request for member 'setRawHeader' in 'request', which is of non-class type 'QNetworkRequest()'

For some reason, the compiler considers this to be a function type declaration. Here is another example
QString s = "test";
QNetworkRequest request(QUrl(s));
request.setRawHeader("", "");

error: request for member 'setRawHeader' in 'request', which is of non-class type 'QNetworkRequest(QUrl)

Working options:
QUrl reqUrl(url);
QNetworkRequest req(reqUrl);

QNetworkRequest req(QUrl("some_url"));

G++ 4.9.2.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Armenian Radio, 2015-01-10
@gbg

The first example is a classic mistake, the compiler is right - you actually wrote a function.
Parentheses are not required to declare a class and call the default constructor.
Even Carmack rode this one :
The fact that ScopedLock lock() is a function declaration and not a parameterless constructor call made me more upset today.

T
tsarevfs, 2015-01-10
@tsarevfs

Unfortunately, for the compiler, the second case also looks like a function declaration.
The req function accepting one parameter (for more details, follow the link).
The first option you suggested is a great way out of this situation. You can also write like this:
Upd: I found a detailed description of this error from Meyers.

T
TriKrista, 2015-01-11
@TriKrista

In the first case, you declared a function, not a class object.
In the second case, there is no such function (setRawHeader(QSting, QString)).
Try like this:

QString s = "test";
QNetworkRequest request(QUrl(s));
request.setRawHeader(QByteArray(""), QByteArray(""));

or like this:
QNetworkRequest request;
request.setRawHeader("", "");

A
Alexey, 2015-01-11
@MAKAPOH

You can use the new universal initialization syntax , ie write curly brackets instead of parentheses. Here 's a note about the difference between x1 and x2 declarations:

T x1; 
T x2();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question