Reputation: 1432
I am trying to create an A* search for a 2d array of integers in c++ (im very new to c++ but i have done one in Java loads of times) the problem is when i trying to push items into the queue it throws a compilation error due a type conflict.
My Node.h is defined as:
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;
}
};
Node.cpp:
#include <stdlib.h>
#include "Node.h"
Node::Node(){
this->xCoord = -1;
this->yCoord = -1;
this->value = -1;
this->parent = NULL;
}
Node::Node(int _x, int _y, int _value){
this->xCoord = _x;
this->yCoord = _y;
this->value = _value;
this->parent = NULL;
}
void Node::setParent(Node* par){
this->parent = par;
}
int Node::getX(){
return xCoord;
}
int Node::getY(){
return yCoord;
}
int Node::getValue(){
return value;
}
Node* Node::getParent(){
return parent;
}
bool Node::operator()(Node& node1, Node& node2) {
return node1.value > node2.value;
}
and my main:
#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;
}
The compiler says:
error: no matching function for call to ‘std::priority_queue<Node, std::vector<Node>, NodeCompare>::push(Node*&)’
which is to do with the type of Node i am trying to push onto the list (so i believe) i have tried changing my code to the following:
Node node = new Node(1,2,19);
which gives the error:
error: conversion from ‘Node*’ to non-scalar type ‘Node’ requested
and i have tried passing in all the variations i know:
openList.push(&node);
and
openList.push(*node);
But they also throw compilation errors.
Can someone explain what i am doing wrong?
Cheers, Chris.
Upvotes: 0
Views: 642
Reputation: 699
You're trying to add a node pointer to a list of node objects. What I would recommend is openList.push(*node);
.
Upvotes: 1
Reputation: 2137
new
returns a pointer to an object while your priority queue is declared to hold
actual objects. You can change main
like so to just deal with a Node
object rather than
a pointer to a Node
on the heap:
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;
}
Upvotes: 1