Visual studio 错误 C2027
Visual studio error C2027
我正在开发我的游戏引擎,我遇到了一些我不知道如何修复的错误。
我有 Camera,它有指向 GameObject 的指针,但每当我使用他时,它都说它是未定义类型的 GameObject。我在 Visual studio 2017 年工作,它给我错误 C2027。
我已经在构造函数中进行了前向声明和指针分配给对象。我看这个页面但是
感谢您的帮助。
这里是camera.h
#pragma once
#include <SDL.h>
#include <SDL_image.h>
#include <map>
#include <algorithm>
class GameObject;
#include "Camera_manager.h"
#include "gameObject.h"
#include "Interface_SDL.h"
#include "Layer_manager.h"
#include "Vector2f.h"
#include "Typedef.h"
class Camera : public Interface_SDL
{
public:
Camera(float relativeX, float relativeY, GameObject* ownerObject, ScreenDestination screenDestination, float cameraResolutionWidth, float cameraResolutionHeight, Layer_manager* layerManager);
~Camera();
//Pozice kamery
Vector2f _relativeLocation;
//Object kde je umístěna
GameObject* _ownerObject;
bool active;
float _cameraResolutionWidth;
float _cameraResolutionHeight;
inline float getCameraResolutionWidth() { return _cameraResolutionWidth; }
inline float getCameraResolutionHeight() { return _cameraResolutionHeight; }
//Getter pro rozměry kamery v rozmezí 0 - 1
float getCameraWidthInPercent() { return _screenDestination.XEnd - _screenDestination.XStart; }
float getCameraHeightInPercent() { return _screenDestination.YEnd - _screenDestination.YStart; }
//Vrací pozici na obrazovce
inline float getScreenXstart() {return _screenDestination.XStart * Interface_SDL::_windowWidth; }
inline float getScreenYstart() { return _screenDestination.YStart * Interface_SDL::_windowHeight; }
ScreenDestination _screenDestination;
Layer_manager* _layerManager;
//getter of LayerManager
Layer_manager* getLayerManger() { return _layerManager;}
inline Vector2f getWorldPos() { return _relativeLocation + _ownerObject->getWorldLocation(); }
inline float getWorldPosX() { return _relativeLocation.GetX() + _ownerObject->getWorldLocation().GetX(); }
inline float getWorldPosY() { return _relativeLocation.GetY() + _ownerObject->getWorldLocation().GetY(); }
SDL_Texture* getTexture(int layerNumber);
//Věco pro mazání textur
SDL_Texture* cleaningTexture;
SDL_Rect* cleaningRect;
//LAYERY
SDL_Texture* textureLayer0;
SDL_Texture* textureLayer1;
SDL_Texture* textureLayer2;
SDL_Texture* textureLayer3;
SDL_Texture* textureLayer4;
SDL_Texture* textureLayer5;
SDL_Texture* textureLayer6;
SDL_Texture* textureLayer7;
SDL_Texture* textureLayer8;
SDL_Texture* textureLayer9;
//Funkce pro správu textur
//Vyčistí všechny Layery a nastaví je na NO USED
void clearAllLayers();
//Vyčistí texturu která se vloží
void clearTexture(SDL_Texture* texture);
//resize layers
void resizeLayers(int width, int height);
void handleEvents();
void update();
};
您不能在 header 文件中使用 _ownerObject
。
原因是你只转发声明它的类型使用class GameObject;
。前向声明只告诉编译器关于 class 的存在,而不是它的样子(这将由 include "GameObject.h" 完成)。因此编译器无法知道 _ownerObject->getWorldLocation()
在你的 header 行
return _relativeLocation + _ownerObject->getWorldLocation();
是一个有效的函数调用。
要解决此问题,请将所有使用 _ownerObject
的函数移动到 cpp 文件中。
编译器显然是对的:D.
你有一个 GameObject
的前瞻性声明很好。不好的地方在这里:
inline Vector2f getWorldPos() { return _relativeLocation + _ownerObject->getWorldLocation(); }
编译器怎么知道_ownerObject
有一个方法getWorldLocation
?
您需要或包含 GameObject
在 cpp 文件中起作用的定义或 delcare 并包含正确的 headers.
我正在开发我的游戏引擎,我遇到了一些我不知道如何修复的错误。 我有 Camera,它有指向 GameObject 的指针,但每当我使用他时,它都说它是未定义类型的 GameObject。我在 Visual studio 2017 年工作,它给我错误 C2027。
我已经在构造函数中进行了前向声明和指针分配给对象。我看这个页面但是 感谢您的帮助。
这里是camera.h
#pragma once
#include <SDL.h>
#include <SDL_image.h>
#include <map>
#include <algorithm>
class GameObject;
#include "Camera_manager.h"
#include "gameObject.h"
#include "Interface_SDL.h"
#include "Layer_manager.h"
#include "Vector2f.h"
#include "Typedef.h"
class Camera : public Interface_SDL
{
public:
Camera(float relativeX, float relativeY, GameObject* ownerObject, ScreenDestination screenDestination, float cameraResolutionWidth, float cameraResolutionHeight, Layer_manager* layerManager);
~Camera();
//Pozice kamery
Vector2f _relativeLocation;
//Object kde je umístěna
GameObject* _ownerObject;
bool active;
float _cameraResolutionWidth;
float _cameraResolutionHeight;
inline float getCameraResolutionWidth() { return _cameraResolutionWidth; }
inline float getCameraResolutionHeight() { return _cameraResolutionHeight; }
//Getter pro rozměry kamery v rozmezí 0 - 1
float getCameraWidthInPercent() { return _screenDestination.XEnd - _screenDestination.XStart; }
float getCameraHeightInPercent() { return _screenDestination.YEnd - _screenDestination.YStart; }
//Vrací pozici na obrazovce
inline float getScreenXstart() {return _screenDestination.XStart * Interface_SDL::_windowWidth; }
inline float getScreenYstart() { return _screenDestination.YStart * Interface_SDL::_windowHeight; }
ScreenDestination _screenDestination;
Layer_manager* _layerManager;
//getter of LayerManager
Layer_manager* getLayerManger() { return _layerManager;}
inline Vector2f getWorldPos() { return _relativeLocation + _ownerObject->getWorldLocation(); }
inline float getWorldPosX() { return _relativeLocation.GetX() + _ownerObject->getWorldLocation().GetX(); }
inline float getWorldPosY() { return _relativeLocation.GetY() + _ownerObject->getWorldLocation().GetY(); }
SDL_Texture* getTexture(int layerNumber);
//Věco pro mazání textur
SDL_Texture* cleaningTexture;
SDL_Rect* cleaningRect;
//LAYERY
SDL_Texture* textureLayer0;
SDL_Texture* textureLayer1;
SDL_Texture* textureLayer2;
SDL_Texture* textureLayer3;
SDL_Texture* textureLayer4;
SDL_Texture* textureLayer5;
SDL_Texture* textureLayer6;
SDL_Texture* textureLayer7;
SDL_Texture* textureLayer8;
SDL_Texture* textureLayer9;
//Funkce pro správu textur
//Vyčistí všechny Layery a nastaví je na NO USED
void clearAllLayers();
//Vyčistí texturu která se vloží
void clearTexture(SDL_Texture* texture);
//resize layers
void resizeLayers(int width, int height);
void handleEvents();
void update();
};
您不能在 header 文件中使用 _ownerObject
。
原因是你只转发声明它的类型使用class GameObject;
。前向声明只告诉编译器关于 class 的存在,而不是它的样子(这将由 include "GameObject.h" 完成)。因此编译器无法知道 _ownerObject->getWorldLocation()
在你的 header 行
return _relativeLocation + _ownerObject->getWorldLocation();
是一个有效的函数调用。
要解决此问题,请将所有使用 _ownerObject
的函数移动到 cpp 文件中。
编译器显然是对的:D.
你有一个 GameObject
的前瞻性声明很好。不好的地方在这里:
inline Vector2f getWorldPos() { return _relativeLocation + _ownerObject->getWorldLocation(); }
编译器怎么知道_ownerObject
有一个方法getWorldLocation
?
您需要或包含 GameObject
在 cpp 文件中起作用的定义或 delcare 并包含正确的 headers.