C++ 在类中调用模板

C++ 在类中调用模板,c++,templates,C++,Templates,我希望创建一个多个类可以使用的静态bool模板函数。我使用这个函数作为比较器来对点向量进行排序。这就是我到目前为止所做的: h班 给我一个错误: 错误:没有用于调用的匹配函数 '排序(std::vector::iterator,std::vector::iterator, )" 有人知道我做错了什么吗?语法是: std::sort(p2Vec.begin(), p2Vec.end(), &Point2D::sortAscending<Point2D>); 并使用它: std

我希望创建一个多个类可以使用的静态bool模板函数。我使用这个函数作为比较器来对点向量进行排序。这就是我到目前为止所做的:

h班

给我一个错误:

错误:没有用于调用的匹配函数 '排序(std::vector::iterator,std::vector::iterator, )"

有人知道我做错了什么吗?

语法是:

std::sort(p2Vec.begin(), p2Vec.end(), &Point2D::sortAscending<Point2D>);
并使用它:

std::sort(p2Vec.begin(), p2Vec.end(), LessByGetX{});
或者,您可以直接使用lambda:

std::sort(p2Vec.begin(), p2Vec.end(), [](const T& lhs, const T& rhs)
    {
        return lhs.getX() < rhs.getX();
    });
排序(p2Vec.begin(),p2Vec.end(),[](常量T&lhs,常量T&rhs) { 返回lhs.getX()语法应该是:

std::sort(p2Vec.begin(), p2Vec.end(), &Point2D::sortAscending<Point2D>);
并使用它:

std::sort(p2Vec.begin(), p2Vec.end(), LessByGetX{});
或者,您可以直接使用lambda:

std::sort(p2Vec.begin(), p2Vec.end(), [](const T& lhs, const T& rhs)
    {
        return lhs.getX() < rhs.getX();
    });
排序(p2Vec.begin(),p2Vec.end(),[](常量T&lhs,常量T&rhs) { 返回lhs.getX()在此处使用lambda函数,如下所示:

std::sort(p2Vec.begin(),p2Vec.end(),
          [](const Point2D & p1, const Point2D & p2) {
        return Point2D::sortAscending( p1, p2); 
      });

请参见此处使用lambda函数,如下所示:

std::sort(p2Vec.begin(),p2Vec.end(),
          [](const Point2D & p1, const Point2D & p2) {
        return Point2D::sortAscending( p1, p2); 
      });

请参见

排序排序
不是排序而是比较。是的,我知道它是比较。对不起,我奇怪的命名惯例……我现在说的与所问的问题无关,而是一条建议;我建议将x和y放在一个数组中:
cpp int-coord[2/*x*/][2/*y*/]={{1,0},{0,1}
sortAscending
不是排序而是比较。是的,我知道这是比较。对不起,我奇怪的命名惯例……我现在说的与所问的问题无关,而是一条建议;我建议将x和y放在一个数组中:
cpp int-coord[2/*x*/][2/*y*/]={{1,0},{0,1}谢谢你的帮助!现在我将尝试所有方法。对于第一种方法,我遇到了一个
错误:无法将“(a.Point2D::getX()
)。。另外,对于第二种方法,我是否将结构存储在.h文件或.cpp文件中?返回类型也必须更改为
bool
,而不是
T
。感谢您的帮助!现在我将尝试所有方法。对于第一种方法,我遇到了一个
错误:无法将“(a.Point2D::getX()
)。。另外,对于第二种方法,我是否将结构存储在.h文件或.cpp文件中?返回类型也必须更改为
bool
,而不是
T
。感谢您的帮助,但我刚刚意识到我的模板函数在Point2D类之外无法工作,因为您无法虚拟模板函数。。我希望我能在有x坐标的Point3D上实现这一点。。。尽管如此,感谢您的努力。感谢您的帮助,但我刚刚意识到我的模板函数在Point2D类之外无法工作,因为您无法虚拟模板函数。。我希望我能在有x坐标的Point3D上实现这一点。。。不过,谢谢你的努力。