[英]C++ undefined error for using function in .cpp file that is defined in a different .cpp file?
I need to use the Display function defined in Dungeon.cpp (with a header in Dungeon.h) in DungeonLayer.cpp.我需要在 DungeonLayer.cpp 中使用 Dungeon.cpp 中定义的 Display 函数(在 Dungeon.h 中有一个标题)。 Visual studio is giving the error message: identifier "Display" is undefined Visual Studio 给出错误消息:标识符“显示”未定义
//DungeonLayer.h
#ifndef DUNGEON_LAYER
#define DUNGEON_LAYER
#include "Dungeon.h"
#include <iostream>
class dungeonLayer
public:
void setItem();
#endif
//DungeonLayer.cpp
#include "DungeonLayer.h"
void dungeonLayer::setItem()
{
Display(); //error, visual studio says Display() is undefined
}
//Dungeon.h
#include <iostream>
#ifndef DUNGEON_H
#define DUNGEON_H
class Dungeon
public:
void Display();
#endif
//Dungeon.cpp
#include "Dungeon.h"
void Dungeon::Display()
{
std::cout << "Goblin";
}
Since Display()
is declared as a member function, so you need an instance of Dungeon
in order to operate it.由于Display()
被声明为成员函数,因此您需要一个Dungeon
实例才能对其进行操作。 Your code should look something like:您的代码应该类似于:
dungeon.Display();
Where dungeon
was previously initialized as Dungeon dungeon
.其中dungeon
之前被初始化为Dungeon dungeon
。
If Display()
is not supposed to be a member function you must declare it as a static function so it can be called independently.如果Display()
不应该是成员函数,则必须将其声明为静态函数,以便可以独立调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.