【问题标题】:Static function in a Class gives the error: two or more data types in declaration of类中的静态函数给出错误:声明中的两个或多个数据类型
【发布时间】:2017-12-10 04:15:03
【问题描述】:

我尝试使用自定义比较函数并在排序和堆中使用它。现在类节点使用operatir

class Node{
public:
    int id;
    int dist;
    Node(int node_id, int fdist = -1):
        id (node_id),
        dist (fdist)
        {};
    bool operator<(const Node & other) const{
        return dist > other.dist;
    }

    static bool Cmpr(Node a, Node b){
        return a->dist > b->dist;
    }   
};

我总是收到以下错误:

file.cpp on line 14:36: error: two or more data types in declaration of 'Cmpr'
 static bool Cmpr(Node a, Node b){

我的代码有什么问题?

【问题讨论】:

  • s/static bool Cmpr(Node a, Node b){/static bool Cmpr(Node* a, Node* b){
  • 啊,感恩的 sed。更喜欢哈雷和他的彗星经典 awk 全天候。

标签: c++ class sorting compare


【解决方案1】:

我不知道这是否是您测试的确切代码,但我看到另一个错误,编译器会抱怨。在:

static bool Cmpr(Node a, Node b){
    return a->dist > b->dist;
}

您将节点 a 和 b 视为指针,但它们不是。如果我将其更改为:

static bool Cmpr(Node a, Node b){
    return a.dist > b.dist;
}

编译时完全没有错误。

【讨论】:

  • 谢谢,杀!你说的对!我更改了很多代码以检查是否可以解决我的问题。忘了恢复它!我在本地机器上检查并编译,没有错误!只有在我尝试 CodeFights 时才会出现错误!显然,它一般都有静态方法的问题!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 2010-09-18
相关资源
最近更新 更多