Question

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?

Était-ce utile?

La solution

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.

Autres conseils

Replace:

std::wstring timeString();

with:

std::wstring timeString;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top