Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++;试图将优先级队列与自定义类一起使用时,编译器抛出错误:为什么尝试填充队列 我试图在C++中创建一个二维数组的整数(我对C++非常陌生,但是我已经在java中加载了很多次),问题是当我试图把项目推到队列中时,它会因为类型冲突而导致编译错误。_C++ - Fatal编程技术网

C++;试图将优先级队列与自定义类一起使用时,编译器抛出错误:为什么尝试填充队列 我试图在C++中创建一个二维数组的整数(我对C++非常陌生,但是我已经在java中加载了很多次),问题是当我试图把项目推到队列中时,它会因为类型冲突而导致编译错误。

C++;试图将优先级队列与自定义类一起使用时,编译器抛出错误:为什么尝试填充队列 我试图在C++中创建一个二维数组的整数(我对C++非常陌生,但是我已经在java中加载了很多次),问题是当我试图把项目推到队列中时,它会因为类型冲突而导致编译错误。,c++,C++,My Node.h定义为: class Node{ private: int xCoord; int yCoord; int value; Node* parent; public: Node(); Node(int x, int y, int value); void setParent(Node* parent); int getX(); int getY(); int getValue(); N

My Node.h定义为:

class Node{

private:

    int xCoord;
    int yCoord;
    int value;
    Node* parent;

public:

    Node();
    Node(int x, int y, int value);
    void setParent(Node* parent);
    int getX();
    int getY();
    int getValue();
    Node* getParent();
    bool operator()(Node& node1, Node& node2);

};

struct NodeCompare {

    bool operator()(Node& node1, Node& node2) const
    {
        int node1value = node1.getValue();
        int node2value = node2.getValue();
        return node1value < node2value;
    }
};
}

我的主要观点是:

#include <iostream>
#include <ostream>
#include <vector>
#include <queue>

#include "Node.h"

int main(){

    using namespace std;

    priority_queue<Node, vector<Node>, NodeCompare> openList;

    Node* node = new Node(1,2,19);

    openList.push(node);

    cout << "node is: x " << node->getX() << " y " << node->getY() << " value "
            << node->getValue() << endl;



    return 0;

}
这就产生了错误:

error: conversion from ‘Node*’ to non-scalar type ‘Node’ requested
我试着把我所知道的所有变化都传过来:

openList.push(&node);

但它们也会抛出编译错误

有人能解释一下我做错了什么吗

干杯,
Chris。

new
在优先级队列被声明为保持时返回指向对象的指针 实物。您可以像这样更改
main
,只处理
节点
对象,而不是 指向堆上
节点的指针:

int main(){

    using namespace std;

    priority_queue<Node, vector<Node>, NodeCompare> openList;

    Node node = Node(1,2,19);

    openList.push(node);

    cout << "node is: x " << node.getX() << " y " << node.getY() << " value "
            << node.getValue() << endl;



    return 0;

}
intmain(){
使用名称空间std;
优先级队列开放列表;
节点=节点(1,2,19);
push(节点);

cout您正在尝试添加指向节点对象列表的节点指针。我建议您使用
openList.push(*node);

Does
openList.push(*node)
引发另一个编译错误?在最初的尝试中,当优先级队列正在查找
节点时,您正在提供指向
节点的指针。首先,您正在创建一个节点指针,并试图将其推入需要节点的队列中。您的代码更改是声明一个节点并尝试分配一个节点指针您可以将队列和向量更改为保留Node*,但必须更改比较函数以获取节点指针,而不是节点。嗯……我从命令行编译,我的代码使用openList.push(*Node)按预期工作。不过,感谢您提供的信息,我将对此进行研究。
Node node = new Node(1,2,19);
error: conversion from ‘Node*’ to non-scalar type ‘Node’ requested
openList.push(&node);
openList.push(*node);
int main(){

    using namespace std;

    priority_queue<Node, vector<Node>, NodeCompare> openList;

    Node node = Node(1,2,19);

    openList.push(node);

    cout << "node is: x " << node.getX() << " y " << node.getY() << " value "
            << node.getValue() << endl;



    return 0;

}