views:

255

answers:

3
include/TestBullet.h:12: error: expected constructor, destructor, or type conver
sion before '(' token

I hate C++ error messages... lol ^^

Basically, I'm following what was written in this post to try to create a factory class for bullets so they can be instantiated from a string, which will be parsed from an xml file, because I don't want to have a function with a switch for all of the classes because that looks ugly.

Here is my TestBullet.h:

#pragma once

#include "Bullet.h"
#include "BulletFactory.h"

class TestBullet : public Bullet {
public:
    void init(BulletData& bulletData);
    void update();
};

REGISTER_BULLET(TestBullet);  <-- line 12

And my BulletFactory.h:

#pragma once

#include <string>
#include <map>
#include "Bullet.h"

#define REGISTER_BULLET(NAME) BulletFactory::reg<NAME>(#NAME)
#define REGISTER_BULLET_ALT(NAME, CLASS) BulletFactory::reg<CLASS>(NAME)

template<typename T> Bullet * create() { return new T; }

struct BulletFactory {
    typedef std::map<std::string, Bullet*(*)()> bulletMapType;
    static bulletMapType map;

    static Bullet * createInstance(char* s) {
        std::string str(s);
        bulletMapType::iterator it = map.find(str);
        if(it == map.end())
            return 0;
        return it->second();
    }

    template<typename T> 
    static void reg(std::string& s) { 
        map.insert(std::make_pair(s, &create<T>));
    }
};

Thanks in advance.

And unrelated to the error, but is there a way to let Bullet include BulletFactory without creating tons of errors (because of circular inclusion)? This way I would be able to remove #include "BulletFactory.h" from the top of all of the bullet subclasses.

+4  A: 

I don't think you can call functions outside of functions (as long as you don't use the result to initialize a global).

UncleBens
worked when I moved the call into the init function.... thanks
jonathanasdf
@jon: You don't want to do that. Then you're registering every time you make a new instance of a bullet.
GMan
ah yes. Forgot about that.. where would be the best place to put it then?
jonathanasdf
@UncleBens: why are global function calls not allowed?
Lazer
+2  A: 

reg() is a function. You can't call a function without a scope.

shoosh
+2  A: 

Here's how you get what you want. (Not using your code, exactly, skips including headers, etc. Just for the idea.):

// bullet_registry.hpp
class bullet;

struct bullet_registry
{
    typedef bullet* (*bullet_factory)(void); 

    std::map<std::string, bullet_factory> mFactories;
};

bullet_registry& get_global_registry(void);

template <typename T>
struct register_bullet
{
    register_bullet(const std::string& pName)
    {
        get_global_registry().mFactories.insert(std::make_pair(pName, create));
    }

    static bullet* create(void)
    {
        return new T();
    }
};

#define REGISTER_BULLET(x) \
        namespace \
        { \
            register_bullet _bullet_register_##x(#x); \
        }

// bullet_registry.cpp
bullet_registry& get_global_registry(void)
{
    // as long as this function is used to get
    // a global instance of the registry, it's
    // safe to use during static initialization
    static bullet_registry result;

    return result; // simple global variable with lazy initialization
}

// bullet.hpp
struct my_bullet : bullet { };

// bullet.cpp
REGISTER_BULLET(my_bullet)

This works by making a global variable, which will be initialized at some point during static initialization. When that happens, in its constructor it accesses the global registry and registers it with the name, and the function used to create bullets.

Since static initialization order is unspecified, we put the global manager in a function, so when that function is called the first time the manager is created on-demand and used. This prevents us from using an uninitialized manager, which could be the case if it were a simple global object.

Free free to ask for clarifications.

GMan
Thanks. This was really clear
jonathanasdf