我正试图在c ++中通过引用传递一个对象。我收到这些错误:
错误1错误C2061:语法错误:标识符'Common'graphics.h 6 1 SDLGameDev
错误2错误C2511:'void Graphics :: CreateWindow(Common&)':'Graphics'中找不到重载的成员函数4 1 SDLGameDev
我找到了关于这个领域的答案,但没有涉及如何做到这一点:
object1.someFunction(object2);
这是我的代码:
// COMMON.H
#ifndef COMMON_H
#define COMMON_H
#include "SDL.h"
#include "iostream"
class Common{
public:
void Init();
bool GetGameRunState(){ return GameRunState; }
void SetGameRunState(bool x){ GameRunState = x; }
private:
bool GameRunState;
};
#endif
// Commmon.cpp
#include "Common.h"
void Common::Init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
SetGameRunState(true);
}
else
{
SetGameRunState(false);
}
}
//有Graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
class Graphics{
public:
void CreateWindow(Common & co);
};
#endif
// Graphics.cpp
#include "Graphics.h"
#include "Common.h"
void Graphics::CreateWindow(Common & co)
{
if (co.GetGameRunState() == true)
{
std::cout << "TEST for CreateWindow()\n";
}
}
// main.cpp中
#include "Common.h"
#include "Graphics.h"
Common co;
Graphics go;
int main(int argc, char * args[])
{
co.Init();
go.CreateWindow(co);
while (co.GetGameRunState() == true)
{
std::cout << "Game is running\n";
SDL_Delay(2000);
break;
}
return 0;
}
答案 0 :(得分:0)
您还没有在Graphics.h文件中包含Common.h,因此它不了解该类。
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "Common.h" // You need this line
class Graphics {
public:
void CreateWindow(Common & co);
};
#endif
答案 1 :(得分:0)
我建议使用单例并将sdl的初始化,渲染器和窗口等的创建放在一个类中。您的问题已经得到解答。