direct initialization of a POD variable does not work but copy initialization does when pushing the variable onto a vector

StackOverflow https://stackoverflow.com/questions/11994243

Вопрос

Why does the following code fail to compile, while the two examples after compile successfully? I'm using VS 2008 on Windows 7.


Direct initialization of POD (fails):

int pod();
std::vector<int> pods;
//pods.push_back(pod); // This will generate a compiler error
// Compile error: 1>c:\test.hpp(43) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'int (__cdecl *)(void)' to 'const int &'

Copy initialization of POD (succeeds:

int pod = int();
std::vector<int> pods;
pods.push_back(pod); // No error!
Это было полезно?

Решение

Look up "most vexing parse" (it has been discussed over and over again here, too).

int pod(); // this does not declare (nor define) an integer

By the way, why did you put that 1 in the MyClass example?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top