C++编译错误:未定义标识符(对于函数参数)

发布于 2024-12-29 08:56:10 字数 1600 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-01-05 08:56:10

乍一看,此错误有两个可能的原因:

最明显的一个:
您的类定义不完整,应该是

class MyClass
{
public:
    unsigned int x; 
    unsigned int y; 

    MyClass() { x = y = 0; }
    MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }

    float Calculate( MyClass &myClass );
}; // semicolon

float MyClass::Calculate( MyClass &myClass )
{
     if(x<myClass.x)
             ....//treatment
}

另外,请确保您的源文件 (.cpp) 包含头文件 (.h),

因此这两个文件将如下所示:

// .h file
class MyClass
{
public:
    unsigned int x; 
    unsigned int y; 

    MyClass() { x = y = 0; }
    MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }

    float Calculate( MyClass &myClass );
};

// .cpp file
#include "MyClass.h"
//definition of constructors
float MyClass::Calculate( MyClass &myClass )
{
   if(x<myClass.x)
             ....//treatment

}

On the first look there are 2 possible reasons for this error:

The most obvious one:
Your class definition is incomplete, it should be

class MyClass
{
public:
    unsigned int x; 
    unsigned int y; 

    MyClass() { x = y = 0; }
    MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }

    float Calculate( MyClass &myClass );
}; // semicolon

float MyClass::Calculate( MyClass &myClass )
{
     if(x<myClass.x)
             ....//treatment
}

Additionally, make sure your source file (.cpp) includes your header file (.h)

So both files would look like that:

// .h file
class MyClass
{
public:
    unsigned int x; 
    unsigned int y; 

    MyClass() { x = y = 0; }
    MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }

    float Calculate( MyClass &myClass );
};

// .cpp file
#include "MyClass.h"
//definition of constructors
float MyClass::Calculate( MyClass &myClass )
{
   if(x<myClass.x)
             ....//treatment

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文