[Qt-interest] Weird compiler error with QString(QChar(value))

K. Frank kfrank29.c at gmail.com
Thu May 6 20:39:53 CEST 2010


Hello -

On Thu, May 6, 2010 at 10:51 AM, Nikos Chantziaras <realnc at arcor.de> wrote:
> On 05/06/2010 05:35 PM, Stephen Chu wrote:
>> In article<201005052127.23761.thiago at kde.org>,
>>   Thiago Macieira<thiago at kde.org>  wrote:
>>> Em Quarta-feira 5. Maio 2010, às 20.51.00, Nikos Chantziaras escreveu:
>>>> Not sure I understand why this won't compile:
>>>>
>>>>     int unicodeValue = func_that_returns_a_unicode_value();
>>>>     QString s(QChar(unicodeValue));
>>>>     const QByteArray&  utf8 = s.toUtf8();  //<-- error happens here
>>>>
>>>> I get:
>>>>
>>>> error: request for member 'toUtf8' in 's', which is of non-class type
>>>> 'QString(QChar)'
>>>>
>>>> I'm confused as hell :P

There is a good explanation for this in Marshall Cline's "C++ FAQ LITE":

   http://www.parashift.com/c++-faq-lite/index.html
   http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19

(For those who haven't seen it before, the C++ FAQ LITE is an excellent
reference, and well worth a look.)

>>> This is one of the things you just have to learn. This line:
>>>
>>>     QString s(QChar(unicodeValue));
>>>
>>> is not what you think.

As Thiago says, this is just one of those things -- something of a C++
 trap that
most of us get bit by at one point or another.

>>> You thought you were declaring an object of class QString and calling one of
>>> its constructors with a QChar (which in turn was initialised with an int).
>>>
>>> The compiler understood that you were declaring a pointer to a function that
>>> returns QString and takes a QChar as a parameter.
>>> ...
>> Thanks for the explanation. I ran into the same problem just last night.
>> Pulling my hairs out on this one.
>>
>> One question though. If that line of code actually declares a function,
>> what does 'unicodeValue' in the declaration do? I thought a function
>> declaration is something like:
>>
>>      QString s(QChar unicodeValue);
>
> It's the same since you can put parentheses pretty much everywhere...
> ...
> That means that these two function declarations:
>
>   QString s(QChar unicodeValue);
>   QString s(QChar(unicodeValue));
>
> are equivalent...

Of course, putting parentheses pretty much everywhere can change the meaning
of your code...

If you want to make your head hurt even more, you can use parentheses to fix the
problem.  Copied from the FAQ LITE:

   void yourCode() {
     Foo x((Bar()));
              ^-----^----- ← These parens save the day
     x.blah();          ← Ahhhh, this works now — no more error messages
     ...
   }

I tried this out with the original code:

   int unicodeValue = 17;
   // QString s(QChar(unicodeValue));   // original code that didn't work
   QString s((QChar(unicodeValue)));    // the extra parentheses from
the FAQ LITE
   const QByteArray&  utf8 = s.toUtf8();  // no more error here --
code compiles fine

Well, for all its virtues, C++ does have its little issues...

Best.


K. Frank




More information about the Qt-interest-old mailing list