Domanda

How come this fails to compile:

std::wstring timeString();
if (sysTime->wHour < 10) timeString.append(L"hi");

But once I change the first line to be like this:

std::wstring timeString(L"some text");

It works?

È stato utile?

Soluzione

std::wstring timeString();

This is a function declaration, not object declaration. Look it carefully : the function name is timeString which takes no argument, and returns std::wstring.

Just write:

std::wstring timeString;

That should be enough.

Only if you prefer C++11's uniform initialization syntax, you could avoid such accidental error:

std::wstring timeString { L"some text" };  //object!

std::wstring timeString { };  //object, not function declaration!

Hope that helps.

Altri suggerimenti

Replace:

std::wstring timeString();

with:

std::wstring timeString;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top