【发布时间】:2011-09-14 14:03:21
【问题描述】:
有谁知道为什么这段代码不能用 VC++ 2010 编译
class C
{
public:
void M(string t) {}
void M(function<string()> func) {}
};
void TestMethod(function<void()> func) {}
void TestMethod2()
{
TestMethod([] () {
C c;
c.M([] () -> string { // compiler error C2668 ('function' : ambiguous call to overloaded function)
return ("txt");
});
});
}
更新:
完整代码示例:
#include <functional>
#include <memory>
using namespace std;
class C
{
public:
void M(string t) {}
void M(function<string()> func) {}
};
void TestMethod(function<void()> func) {}
int _tmain(int argc, _TCHAR* argv[])
{
TestMethod([] () {
C c;
c.M([] () -> string { // compiler erorr C2668 ('function' : ambiguous call to overloaded function M)
return ("txt");
});
});
return 0;
}
【问题讨论】:
-
好的。那么C2668怎么说呢?不是每个人都在使用那个编译器。
-
对不起。我在上面添加了错误描述
-
我在下面发布的版本可以用 g++ 干净地编译。您应该发布您的实际代码。
-
您的代码仍然不完整。要使用
string,您应该#include <string>。此外,与标准兼容的main具有签名int main()或int main(int, char*[])。其他一切都是编译器特定的。 -
顺便说一句,到目前为止你还尝试过什么?
标签: visual-c++ compiler-construction lambda