关于C++;指针 我刚开始练习C++,我陷入了一个问题。 我有一个节点类,该类有如下构造函数: 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; }
到目前为止一切正常,但我无法创建具有空父节点的节点对象。 我试过这个:关于C++;指针 我刚开始练习C++,我陷入了一个问题。 我有一个节点类,该类有如下构造函数: 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; },c++,pointers,C++,Pointers,到目前为止一切正常,但我无法创建具有空父节点的节点对象。 我试过这个: Node *n = new Node("state name",NULL,0,15,20,1); 我还尝试创建一个新对象并将其指定为父节点,但也没有成功 Node *temp = new Node(); Node *n = new Node("state name",temp,0,15,20,1); 我的指针有问题,但我不知道我遗漏了什么。我得到一个编译错误,表示没有匹配的函数调用 提前感谢您的构造函数是公共的吗 cla
Node *n = new Node("state name",NULL,0,15,20,1);
我还尝试创建一个新对象并将其指定为父节点,但也没有成功
Node *temp = new Node();
Node *n = new Node("state name",temp,0,15,20,1);
我的指针有问题,但我不知道我遗漏了什么。我得到一个编译错误,表示没有匹配的函数调用
提前感谢您的
构造函数是公共的吗
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;
};
另外,不要忘记
节点
声明后的分号,这不是Java;) 我认为为空指针值指定0
是正常的,而不是null
(null
是一个用于C的宏,根据宏的定义方式,它可能无法在C++中正常工作)
另外,我不喜欢将std::string
视为参数类型。我更喜欢看constchar*
,或者conststd::string&
另外
Node*Node=newnode()
将产生该编译器错误,因为当您声明一个非默认构造函数时,您将隐藏默认构造函数(其中“默认构造函数”是指不带参数的构造函数)。如果要支持newnode()代码>然后您需要显式声明默认构造函数(并定义它的实现方式)。在更正拼写错误并实现Node::Node()构造函数后,这对我在Mac OS X上的GCC-4上运行良好。如果您编辑了您的问题,将所有代码包括到测试程序中,以及编译时收到的实际错误消息,这可能会有所帮助。以下编译对我来说很好:
// Added this.
#include <string>
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;
}; // Added semicolon here
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;
}
// Put code inside main.
int main()
{
Node *n = new Node("state name",NULL,0,15,20,1);
}
//添加了这个。
#包括
类节点
{公众:
节点(std::string,Node*,int,int,int,int);
私人:
std::字符串状态;
节点*父节点;
国际总成本;
整数路径成本;
国际单位成本;
智力深度;
}; // 这里加了分号
节点::节点(std::字符串状态,节点*父节点,
整数路径成本,整数启发式成本,整数总成本,整数深度)
{
这个->状态=状态;
此->父节点=父节点;
此->路径成本=路径成本;
这->启发式成本=启发式成本;
此->总成本=总成本;
这个->深度=深度;
}
//把代码放在main里面。
int main()
{
节点*n=新节点(“状态名称”,NULL,0,15,20,1);
}
“谢谢安得烈,我忘记分号:”你得到了什么确切的错误消息?<代码> null <代码>通常是代码> >定义为<代码> > 0代码< >代码> >代码>((空缺*)0)<代码> > C,是的,如果它被定义为<代码>((空洞*)0)< /C++ >,C++将无法工作。OP没有说在他的代码中定义了什么。你永远不应该自己定义它。使用标准标题中提供的定义。Stroustrup倾向于避免使用NULL:hi,抱歉,但是编译器以土耳其语给出错误消息,这对我来说没有任何意义。有人把错误信息翻译成了土耳其语,翻译得很糟糕。好吧,但是你能读英语,对吗?只需安装一个GCC的股票版本,并尝试读取该错误消息。