我刚刚开始练习c ++而且我一度陷入困境.我有一个Node类,类有一个像这样的构造函数:
class Node
{
public:
Node(std::string,Node *,int,int,int,int);
private:
std::string state;
Node* parent_node;
int total_cost;
int path_cost;
int heuristic_cost;
int depth;
}
Node::Node(std::string state,Node *parent_node,int path_cost,int heuristic_cost,int total_cost,int depth)
{
this->state=state;
this->parent_node=parent_node;
this->path_cost=path_cost;
this->heuristic_cost=heuristic_cost;
this->total_cost=total_cost;
this->depth=depth;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切正常,但我无法使用NULL parent_node创建Node对象.我试过这个:
Node *n = new Node("state name",NULL,0,15,20,1);
Run Code Online (Sandbox Code Playgroud)
我也尝试创建一个新对象并将其指定为parent_node,但也没有成功.
Node *temp = new Node();
Node *n = new Node("state name",temp,0,15,20,1);
Run Code Online (Sandbox Code Playgroud)
我做错了指针,但我不知道我错过了什么.我收到一个编译错误,说没有匹配的函数调用.
提前致谢
是你的constructor public?
class Node
{
public:
Node(std::string,Node *,int,int,int,int);
private:
std::string state;
Node* parent_node;
int total_cost;
int path_cost;
int heuristic_cost;
int depth;
};
Run Code Online (Sandbox Code Playgroud)
另外,请不要忘记Node声明后的分号,这不是Java;)