C++编译错误:未定义标识符(对于函数参数)
我有一个 C++ 类 main.cpp,在其中创建了一个如下所示的类:
class MapSearchNode
{
public:
unsigned int x; // the (x,y) positions of the node
unsigned int y;
MapSearchNode() { x = y = 0; }
MapSearchNode( unsigned int px, unsigned int py ) { x=px; y=py; }
float goalDistance( MapSearchNode &nodeGoal );
};
float MapSearchNode::goalDistance( MapSearchNode &nodeGoal )
{
float xd = fabs(float(((float)x - (float)nodeGoal.x)));
float yd = fabs(float(((float)y - (float)nodeGoal.y)));
return xd + yd;
}
int main{
//treatment
}
它工作正常,但后来我想分离 MyClass 类,所以我创建了一个 MyClass.h 和 MyClass.cpp 并分离了如下代码:
MyClass .h
#ifndef _MAPSEARCHNODE_H
#define _MAPSEARCHNODE_H
class MapSearchNode
{
public:
MapSearchNode();
MapSearchNode( unsigned int px, unsigned int py );
public:
unsigned int x;
unsigned int y;
float goalDistance( MapSearchNode &goalNode );
};
#endif
MyClass.cpp
#include "MapSearchNode.h"
MapSearchNode::MapSearchNode():x(0), y(0))
{}
MapSearchNode::MapSearchNode( unsigned int px, unsigned int py ):x(px), y(py)
{}
float MapSearchNode::goalDistance(MapSearchNode &goalNode ){
float xDistance = fabs(float(((float)x - (float)goalNode.x)));
float yDistance = fabs(float(((float)y - (float)goalNode.y)));
return xDistance + yDistance;
}
Bur 当我尝试编译时出现错误:
Undefined identifier goalNode;
//for the function goalDistance
有人可以解释一下为什么我会收到此错误以及如何修复它。
编辑:我希望我现在没有忘记任何事情。
编辑: 非常感谢那些降级的人。我们中的一些人不是像您这样的专家,他们很难发现错误,即使是很小的错误!
I have a C++ class main.cpp in which I created a class like following:
class MapSearchNode
{
public:
unsigned int x; // the (x,y) positions of the node
unsigned int y;
MapSearchNode() { x = y = 0; }
MapSearchNode( unsigned int px, unsigned int py ) { x=px; y=py; }
float goalDistance( MapSearchNode &nodeGoal );
};
float MapSearchNode::goalDistance( MapSearchNode &nodeGoal )
{
float xd = fabs(float(((float)x - (float)nodeGoal.x)));
float yd = fabs(float(((float)y - (float)nodeGoal.y)));
return xd + yd;
}
int main{
//treatment
}
And it works fine but then I wanted to seperate the class MyClass, so I created a MyClass.h and MyClass.cpp and seperated the code like following:
MyClass.h
#ifndef _MAPSEARCHNODE_H
#define _MAPSEARCHNODE_H
class MapSearchNode
{
public:
MapSearchNode();
MapSearchNode( unsigned int px, unsigned int py );
public:
unsigned int x;
unsigned int y;
float goalDistance( MapSearchNode &goalNode );
};
#endif
MyClass.cpp
#include "MapSearchNode.h"
MapSearchNode::MapSearchNode():x(0), y(0))
{}
MapSearchNode::MapSearchNode( unsigned int px, unsigned int py ):x(px), y(py)
{}
float MapSearchNode::goalDistance(MapSearchNode &goalNode ){
float xDistance = fabs(float(((float)x - (float)goalNode.x)));
float yDistance = fabs(float(((float)y - (float)goalNode.y)));
return xDistance + yDistance;
}
Bur when i try to compile i have an error:
Undefined identifier goalNode;
//for the function goalDistance
Can someone please explain me why am I getting this error and how to fix it.
EDIT: I hope I haven't forgotten anything now.
EDIT:
Well thanks a lot to those who downrated. Some of us are not experts like you and it's hard for them to see errors even small ones!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
乍一看,此错误有两个可能的原因:
最明显的一个:
您的类定义不完整,应该是
另外,请确保您的源文件 (.cpp) 包含头文件 (.h),
因此这两个文件将如下所示:
On the first look there are 2 possible reasons for this error:
The most obvious one:
Your class definition is incomplete, it should be
Additionally, make sure your source file (.cpp) includes your header file (.h)
So both files would look like that: