帮我看下为何这里运算符重载失败
#include "stdafx.h"#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
#include <ostream>
using namespace std;
class sumsqplus1
{
public :
sumsqplus1():value(0){};
int operator {}()
{
++value; return value*value + 1;
}
private:
int value;
};
int main( )
{
vector<int> int_vector(4);
generate(int_vector.begin(),int_vector.end(),sumsqplus1{});
for each (int elme in int_vector)
{
cout << elme << " " << endl;
}
return 0;
}这段代码重载了{},不知道为什么总是失败,要换成()才可以成功,有那个高人知道为什么呢?
1>e:\soft\vc++\test\slt_generate\slt_generate\slt_generate.cpp(16) : error C2833: 'operator {' is not a recognized operator or type
1>e:\soft\vc++\test\slt_generate\slt_generate\slt_generate.cpp(16) : error C2059: syntax error : 'newline'
1>e:\soft\vc++\test\slt_generate\slt_generate\slt_generate.cpp(16) : error C2143: syntax error : missing ';' before '}'
1>e:\soft\vc++\test\slt_generate\slt_generate\slt_generate.cpp(16) : error C2238: unexpected token(s) preceding ';'
1>e:\soft\vc++\test\slt_generate\slt_generate\slt_generate.cpp(16) : fatal error C1903: unable to recover from previous error(s); stopping compilation
[ 本帖最后由 lsrwan 于 2010-4-21 23:29 编辑 ]