hu *_*ang 1 c++ compiler-errors operator-overloading
使用mingw32-g ++编译时,有错误:没有匹配函数来调用'for_each(int [9],int*,main():: Output)',但在vs2005中可以做得好吗?
#include <iostream>
#include <algorithm>
int main() {
struct Output {
void operator () (int v) {
std::cout << v << std::endl;
}
};
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::for_each(a, a + sizeof(a)/sizeof(a[0]), Output());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在pre-C++ 11版本中,用作模板参数的语言类型需要具有链接.C++中的本地类声明没有链接,这就是为什么它们不能在C++ 98/C++ 03中用作模板参数的原因.在这些语言版本中,您必须在命名空间范围内声明您的类.
在C++ 11中删除了链接要求.从C++ 11的角度来看,您的代码是有效的.显然,您正在以C++ 11之前的模式进行编译.