質問

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?

役に立ちましたか?

解決

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.

他のヒント

Replace:

std::wstring timeString();

with:

std::wstring timeString;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top