Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对指针向量排序 我试图对C++中的一些节点进行排序,我已经存储在向量中。 bool compare_func(const node* a, const node* b) { return a->getPoint()<b->getPoint(); }_C++_Sorting_Vector - Fatal编程技术网

对指针向量排序 我试图对C++中的一些节点进行排序,我已经存储在向量中。 bool compare_func(const node* a, const node* b) { return a->getPoint()<b->getPoint(); }

对指针向量排序 我试图对C++中的一些节点进行排序,我已经存储在向量中。 bool compare_func(const node* a, const node* b) { return a->getPoint()<b->getPoint(); },c++,sorting,vector,C++,Sorting,Vector,我得到: error C2662: 'node::getStartPoint' : cannot convert 'this' pointer from 'const node' to 'node & error C2662: 'node::getStartPoint' : cannot convert 'this' pointer from 'const node' to 'node &' error C2039: 'sort' : is not a member of 'st

我得到:

error C2662: 'node::getStartPoint' : cannot convert 'this' pointer from 'const node' to 'node &
error C2662: 'node::getStartPoint' : cannot convert 'this' pointer from 'const node' to 'node &'
error C2039: 'sort' : is not a member of 'std'
error C3861: 'sort': identifier not found
我的文件顶部有以下内容:

using namespace std;
std::vector<node*> dataSet;
使用名称空间std;
向量数据集;
提前谢谢

更新: 我重载了getPoint函数,实际上忘记了include算法,[我原以为我在某一点上包含了它]


谢谢

看起来您需要提供
节点::getPoint()
常量
重载:


除此之外,您还需要为
std::sort

包含
头。看起来您需要提供
const
重载
节点::getPoint()


除此之外,还需要为
std::sort

包含
头。前两个错误看起来像是在
常量对象上调用
getStartPoint()
,而成员函数不是
常量。要解决这个问题:

point getStartPoint() const;
                      ^^^^^
第二个是因为您没有包含声明
std::sort

#include <algorithm>
#包括

前两个错误看起来像是在
常量对象上调用
getStartPoint()
,而成员函数不是
const
。要解决这个问题:

point getStartPoint() const;
                      ^^^^^
第二个是因为您没有包含声明
std::sort

#include <algorithm>
#包括
因为您是在
const节点上调用它
,所以只能在const对象上调用声明为const的函数。这样的函数不能更改任何类成员(声明为
mutable
的除外)

声明成员方法会导致函数声明,该声明将成员指针作为第一个参数。 例如:

class node{
public:
    point getStartPoint();
    point getStartPoint(int arg);
};
导致

point node::getStartPoint(node* this);
point node::getStartPoint(node* this, int arg);
point node::getStartPoint(const node* this);
point node::getStartPoint(const node* this, int arg);
但是:

导致

point node::getStartPoint(node* this);
point node::getStartPoint(node* this, int arg);
point node::getStartPoint(const node* this);
point node::getStartPoint(const node* this, int arg);
因此错误

错误C2662:“节点::getStartPoint”:无法转换“this”指针 从“常量节点”到“节点”&

因为您是在
const节点上调用它
,所以只能在const对象上调用声明为const的函数。这样的函数不能更改任何类成员(声明为
mutable
的除外)

声明成员方法会导致函数声明,该声明将成员指针作为第一个参数。 例如:

class node{
public:
    point getStartPoint();
    point getStartPoint(int arg);
};
导致

point node::getStartPoint(node* this);
point node::getStartPoint(node* this, int arg);
point node::getStartPoint(const node* this);
point node::getStartPoint(const node* this, int arg);
但是:

导致

point node::getStartPoint(node* this);
point node::getStartPoint(node* this, int arg);
point node::getStartPoint(const node* this);
point node::getStartPoint(const node* this, int arg);
因此错误

错误C2662:“节点::getStartPoint”:无法转换“this”指针 从“常量节点”到“节点”&


getPoint
和它使用的任何其他函数
const
是否合格?您发布的代码没有引用
getStartPoint
,因此它要么是错误的,要么是不完整的。但是,您要调用的任何方法都必须是const限定的。
getPoint
和它使用的任何其他函数是否为const限定的?您发布的代码没有引用
getStartPoint
,因此它要么是错误的,要么是不完整的。但是,无论您想要调用什么方法,都需要使用const限定。