C++ 使函子成为类成员函数时发生编译错误
我有一个函子,我想和sort()一起使用,这个容器的类型是C++ 使函子成为类成员函数时发生编译错误,c++,visual-studio-2010,stl,functor,C++,Visual Studio 2010,Stl,Functor,我有一个函子,我想和sort()一起使用,这个容器的类型是 std::list<std::pair<unsigned, unsigned>> 错误: 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1324): error C2064: term does not evaluate to a function taking 2 arguments 1>
std::list<std::pair<unsigned, unsigned>>
错误:
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1324): error C2064: term does not evaluate to a function taking 2 arguments
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1394) : see reference to function template instantiation 'void std::list<_Ty>::merge<_Pr3>(std::list<_Ty> &,_Pr3)' being compiled
1> with
1> [
1> _Ty=std::pair<unsigned int,unsigned int>,
1> _Pr3=bool (__thiscall GameBoard::* )(std::pair<unsigned int,unsigned int>,std::pair<unsigned int,unsigned int>)
1> ]
1> GameBoard.cpp(341) : see reference to function template instantiation 'void std::list<_Ty>::sort<bool(__thiscall GameBoard::* )(std::pair<_Ty1,_Ty2>,std::pair<_Ty1,_Ty2>)>(_Pr3)' being compiled
1> with
1> [
1> _Ty=std::pair<unsigned int,unsigned int>,
1> _Ty1=unsigned int,
1> _Ty2=unsigned int,
1> _Pr3=bool (__thiscall GameBoard::* )(std::pair<unsigned int,unsigned int>,std::pair<unsigned int,unsigned int>)
1> ]
1>C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\include\list(1324):错误C2064:term不计算为包含2个参数的函数
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\list(1394):请参阅正在编译的函数模板实例化“void std::list::merge(std::list&,_Pr3)”的参考
1> 与
1> [
1> _Ty=std::pair,
1> _upr3=bool(uu thiscall GameBoard::*)(std::pair,std::pair)
1> ]
1> cpp(341):请参阅正在编译的函数模板实例化“void std::list::sort(_Pr3)”的参考
1> 与
1> [
1> _Ty=std::pair,
1> _Ty1=无符号整数,
1> _Ty2=无符号整数,
1> _upr3=bool(uu thiscall GameBoard::*)(std::pair,std::pair)
1> ]
知道这里出了什么问题吗?
函子需要访问类的私有数据,因此我将其设为成员fn。如果它不是成员fn,则编译良好。我怎样才能解决这个问题
谢谢,不能以这种方式使用成员函数,因为sort()不知道需要调用它的对象。在MSVC10中,最简单的解决方案是lambda
std::sort(..., [&, this] -> bool (std::pair<unsigned, unsigned> left, std::pair<unsigned, unsigned> right) {
return this->SortMoveList(left, right);
});
std::sort(…,[&,this]->bool(std::pair left,std::pair right){
返回此->SortMoveList(左、右);
});
不能以这种方式使用成员函数,因为sort()不知道需要调用它的对象。在MSVC10中,最简单的解决方案是lambda
std::sort(..., [&, this] -> bool (std::pair<unsigned, unsigned> left, std::pair<unsigned, unsigned> right) {
return this->SortMoveList(left, right);
});
std::sort(…,[&,this]->bool(std::pair left,std::pair right){
返回此->SortMoveList(左、右);
});
函子是一个行为类似于函数的对象
这意味着您需要定义一个定义运算符()的类
例如:
class GameBoardMoveListSorter
{
bool operator()(std::pair<unsigned, unsigned> const& left,
std::pair<unsigned, unsigned> const& right) const
{
return left.first < right.first; // or whatever your strict weak ordering is.
}
};
/// STUFF
moveList.sort(GameBoardMoveListSorter());
class GameBoardMoveListSorter
{
布尔运算符()(std::pair const和left,
标准::成对常数(右)常数
{
返回left.first
根据评论进行编辑:
其他人的意见请:
我认为新标准允许内部类访问封闭类的私有成员。但是刚刚重新阅读了这个标准,它似乎不是我看到的措辞(编译器的行为似乎允许访问(尽管我知道这方面的一致性一直很弱))
第9.7节第4段
与成员函数一样,嵌套类中定义的友元函数(11.4)也在该类的词法范围内;它遵守与该类(9.4)的静态成员函数相同的名称绑定规则,但它对封闭类的成员没有特殊访问权限
基于手册的上述章节。内部类必须是友元类才能访问外部类的私有成员
注意。与java不同,内部类和外部类之间没有隐含的父关系。因此,内部类必须具有对外部类对象的显式引用才能访问其成员
#include <memory>
class Chess
{
private:
int board[8][8];
class GameBoardMoveListSorter
{
GameBoardMoveListSorter(Chess& p)
: parent(p)
{}
bool operator()(std::pair<unsigned, unsigned> const& left,
std::pair<unsigned, unsigned> const& right) const
{
int val = parent.board[0][0] + parent.board[7][7];
return left.first + val < right.first - val; // or whatever your strict weak ordering is.
}
Chess& parent;
};
// I believe that it must be a friend to access private members.
friend class GameBoardMoveListSorter;
public:
void makeMove()
{
std::list<std::pair<unsigned, unsigned> > moveList(/*Generate Moves*/);
moveList.sort(GameBoardMoveListSorter(*this));
// Do something with the move list.
}
};
#包括
国际象棋
{
私人:
国际板[8][8];
类GameBoardMoveListSorter
{
GameBoardMoveListSorter(国际象棋和p)
:家长(p)
{}
布尔运算符()(std::pair const和left,
标准::成对常数(右)常数
{
int val=parent.board[0][0]+parent.board[7][7];
返回left.first+val
函子是一个行为类似于函数的对象
这意味着您需要定义一个定义运算符()的类
例如:
class GameBoardMoveListSorter
{
bool operator()(std::pair<unsigned, unsigned> const& left,
std::pair<unsigned, unsigned> const& right) const
{
return left.first < right.first; // or whatever your strict weak ordering is.
}
};
/// STUFF
moveList.sort(GameBoardMoveListSorter());
class GameBoardMoveListSorter
{
布尔运算符()(std::pair const和left,
标准::成对常数(右)常数
{
返回left.first
根据评论进行编辑:
其他人的意见请:
我认为新标准允许内部类访问封闭类的私有成员。但是刚刚重新阅读了这个标准,它似乎不是我看到的措辞(编译器的行为似乎允许访问(尽管我知道这方面的一致性一直很弱))
第9.7节第4段
与成员函数一样,嵌套类中定义的友元函数(11.4)也在该类的词法范围内;它遵守与该类(9.4)的静态成员函数相同的名称绑定规则,但它对封闭类的成员没有特殊访问权限
基于手册的上述章节。内部类必须是友元类才能访问外部类的私有成员
注意。与java不同,内部类和外部类之间没有隐含的父关系。因此,内部类必须具有对外部类对象的显式引用才能访问其成员
#include <memory>
class Chess
{
private:
int board[8][8];
class GameBoardMoveListSorter
{
GameBoardMoveListSorter(Chess& p)
: parent(p)
{}
bool operator()(std::pair<unsigned, unsigned> const& left,
std::pair<unsigned, unsigned> const& right) const
{
int val = parent.board[0][0] + parent.board[7][7];
return left.first + val < right.first - val; // or whatever your strict weak ordering is.
}
Chess& parent;
};
// I believe that it must be a friend to access private members.
friend class GameBoardMoveListSorter;
public:
void makeMove()
{
std::list<std::pair<unsigned, unsigned> > moveList(/*Generate Moves*/);
moveList.sort(GameBoardMoveListSorter(*this));
// Do something with the move list.
}
};
#包括
国际象棋
{
私人:
国际板[8][8];
类GameBoardMoveListSorter
{
GameBoardMoveListSorter(国际象棋和p)
:家长(p)
{}
布尔运算符()(std::pair const