Error: expected constructor, destructor, or type conversion to '(' token

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

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

Basically, I follow what was written in this post to try to create a factory class for the bullet so that they can be created from a string, which will be parsed from an XML file, because I do not want to have a function with a switch for all classes, because it 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 not error-related, but is there a way to let Bullet enable BulletFactory without creating a lot of errors (due to circular inclusion)? That way, I could remove #include "BulletFactory.h"from the top of all subclasses of bullet.

+3
3

, . ( , , , .. .):

// 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)

, , - . , , .

, , , . , , .

.

+2

, ( ).

+4

reg()is a function. You cannot call a function without a scope.

+2
source

Source: https://habr.com/ru/post/1740694/


All Articles