EDIT: The problem was that I was trying to call a static function in the global scope of my cpp files, which didn't work for some reason:
//Player.cpp
RTTI::instance()->registerClass(...) // ERROR
Player::Player() {}
Player::~Player() {}
To solve the problem I changed my code as follows:
template <class T>
struct RTTIRegister {
RTTIRegister<T>(const std::string& name) {
RTTI::instance()->registerClass(name, &T::createInstance);
}
};
#define PROTOTYPE_CREATE(CLASS, BASE) \
static BASE* createInstance() { return new CLASS(); }
#define PROTOTYPE_REGISTER(NAME, CLASS) \
RTTIRegister<CLASS> __rtti__(NAME);
<------------------- END OF EDIT ---------------------->
I am tryting to write a class / macro which will enable me to create instances of classes, whose name I have stored in strings, read from files. I have lots of derived classes of the class GameObject. When I use the code bellow it gives me an error in Visual Studio C++ 2012 Express:
Error 2 IntelliSense: a trailing return type requires the 'auto' type specifier Player.cpp
Error 1 error C2061: syntax error : identifier 'registerClass' player.cpp
Why am I receiving this error and what can I do to fix it?
Code:
// JSON
{
"GameObjects" : [
{
"Class" : "Player",
"Health" : 15
}
]
}
Now when my program reads the file it should create an instance of the class Player. What I currently have is the following:
class RTTI
{
typedef GameObject* (*createFunc)(void);
public:
static RTTI* instance();
void registerClass(const std::string& className, createFunc instantiate);
GameObject* createGameObject(const std::string& className);
private:
static RTTI* s_instance;
std::map<std::string, createFunc> s_registeredClases;
};
#define PROTOTYPE_CREATE(CLASS) \
static CLASS* createInstance() { return new CLASS(); }
#define PROTOTYPE_REGISTER(NAME, CLASS) \
RTTI::instance()->registerClass(NAME, &CLASS::createInstance);
Now my player class is defined as:
//Player.h
class Player : public GameObject
{
PROTOTYPE_CREATE(Player)
}
//Player.cpp
PROTOTYPE_REGISTER("Player", Player) /* **THIS IS WHERE IT GIVES ME AN ERROR */
Player::Player() {}
Player::~Player() {}
After a class has been registered I should be able to create instances by saying RTTI::createGameObject("Player");