我遇到这种情况,我想到测试2类“ Game Object
”和“ Transform
”,它们包含最少数量的参数,以便您更轻松地理解问题。
这里是GameObject
:
#pragma once
#include "string"
#include "Transform.h"
class GameObject
{
public:
std::string name;
std::shared_ptr<Transform> transform;
GameObject(std::string name = "GameObject")
: name(name)
{
this->transform = std::make_shared<Transform>("TransformationFromGameObject");
}
~GameObject() {}
std::shared_ptr<Transform> getTransform() { return this->transform; }
std::string getName() { return this->name; }
};
还有Transform
:
#pragma once
#include "string"
#include "GameObject.h"
struct Vector3 { int x, y, z; };
class Transform
{
public:
std::shared_ptr<GameObject> gameObject;
std::string name;
Vector3 position = { 1, 2, 1 };
Transform(std::shared_ptr<GameObject> gameObjectIn, std::string name = "Transform")
: gameObject(gameObjectIn), name(name)
{
}
~Transform() {}
std::string getName() { return this->name; }
};
我希望从Transform
获得Game Object
组件,并从Game Object
获得Transform
。是的,是的,就像游戏引擎中的游戏对象系统一样,我想做类似的事情。仅当我将std::shader_ptr<Transform>
指针添加到Game object
并包含文件时,所有内容才能正常工作。但是,当我将std::shader_ptr<GameObject>
的指针添加到Transform
并包含文件时,会出现编译错误错误
C2065: 'GameObject': undeclared identifier occurs
和其他
(13) error C2065: 'GameObject': undeclared identifier
(13) error C2923: 'std::shared_ptr': 'GameObject' is not a valid template type argument for parameter '_Ty'
(15) error C2065: 'GameObject': undeclared identifier
(15) error C2923: 'std::shared_ptr': 'GameObject' is not a valid template type argument for parameter '_Ty'
(17) fatal error C1903: unable to recover from previous error(s); stopping compilation
最后是我的Main.cpp文件:
#include <iostream>
#include "string"
#include "GameObject.h"
int main()
{
//getting transform from game object
std::shared_ptr<GameObject> gameObject = std::make_shared<GameObject>("Player");
std::cout << gameObject->getTransform()->getName() << std::endl;
//getting game object from transform component
std::shared_ptr<Transform> transform = std::make_shared<Transform>(gameObject, "SomeTransform");
std::cout << transform->gameObject->getName() << std::endl;
std::cin.get();
}
我做错了什么?我该如何解决?