我的基类(State.h):
#pragma once
#include <Windows.h>
#define WIN32_LEAN_AND_MEAN
#include <gl\GL.h>
#include <gl\GLU.h>
#include "StateManager.h"
class State
{
public:
virtual ~State();
virtual void update(double dt){}
virtual void render(){}
};
派生类(State_Test.h):
#pragma once
#include "State.h"
class State_Test : public State
{
public:
State_Test();
~State_Test();
};
它给出的错误是在另一个类(StateManager.h)中:
#pragma once
#include "State.h"
#include <map>
#include <string>
class StateManager
{
public:
StateManager();
~StateManager();
std::map<std::string, State *> m_StateMap;//error C2976: 'std::map' : too few template arguments | error C2065: 'State' : undeclared identifier | error C2059: syntax error: '>'
std::string m_CurrentState;
void AddState(std::string stateId, State *state);//error C2061: syntax error : identifier 'State'
void ChangeState(std::string stateId);
};
还有这个警告:
Warning 5 warning C4005: '_malloca' : macro redefinition c:\program files (x86)\microsoft visual studio 12.0\vc\include\crtdbg.h 586 1
从语法上看它看起来是正确的,VS没有强调任何东西,而且这段代码之前没有错误。我只是想知道是否有人之前看到过这个错误或知道它的原因是什么?
答案 0 :(得分:2)
您有循环依赖问题:State.h
包含StateManager.h
,其中包含State.h
。
由于StateManager
仅使用指向State
的指针,请删除此包含并转发声明State
:
#pragma once
//#include "State.h" -> remove this
#include <map>
#include <string>
class State; //-> add this
class StateManager
{
public:
StateManager();
~StateManager();
std::map<std::string, State *> m_StateMap;
std::string m_CurrentState;
void AddState(std::string stateId, State *state);
void ChangeState(std::string stateId);
};