如果我编写以下程序,它会按我的预期工作:
struct Foo {
Foo (std::string x) { std::cout << x << std::endl; }
};
int main () { Foo("hello, world"); }
但是,如果我编写一个稍微不同的程序,我会得到一个编译错误:
struct Foo {
Foo (std::string x) { std::cout << x << std::endl; }
};
std::string x("hello, world");
int main () { Foo(x); }
错误是:
prog.cc: In function 'int main()':
prog.cc:10:20: error: no matching function for call to 'Foo::Foo()'
为什么第二个程序而不是第一个程序会出现错误?