【问题标题】:Calling C++ std::sort on a vector of struct pointers在结构指针向量上调用 C++ std::sort
【发布时间】:2016-09-22 04:45:51
【问题描述】:

*已编辑以添加错误消息

当我在指向我之前在程序中定义的结构的指针向量上调用排序函数时,Visual Studio 的智能感知会发出红色标记。

我已经实例化了一个指向我的比较方法的函数指针,并按如下方式调用排序:

bool(*compareNodes)(nodePtr, nodePtr) = compNodes;

sort(frontier.begin(), frontier.end(), compNodes);

/* flags compnodes and sort; "cannot determine which instance of overloaded 
 * function "compNodes is intended" */

compNodes 函数:

static bool compNodes(nodePtr Node1, nodePtr Node2){
    if (Node1->fValue != Node2->fValue)
        return (Node1->fValue < Node2->fValue);
    else
        return (Node1->ID > Node2->ID);
}

【问题讨论】:

  • 而您关心 Intellisense 的功能是因为...?
  • 您有问题吗?
  • 有错误提示吗?
  • 请将错误信息添加到您的问题中。有时,IntelliSense 是错误的,代码编译得很好。
  • 错误信息暗示您的程序中有多个compNodes 函数,请向我们展示另一个。

标签: c++ sorting vector function-pointers


【解决方案1】:

猜测:compNodes 已超载。您巧妙地尝试使用compareNodes 获取指向正确重载的指针,但实际上并没有在std::sort() 中使用它。将行更改为:

sort(frontier.begin(), frontier.end(), compareNodes);

如果您的警告级别足够高,您会看到 compareNodes 当前未使用。

【讨论】:

  • 哇,没听懂。这解决了问题,我也可以这样保持我的函数原型!
猜你喜欢
  • 2013-04-28
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 2015-11-05
  • 2016-03-06
相关资源
最近更新 更多