include/TestBullet.h:12: error: expected constructor, destructor, or type conver
sion before '(' token
我讨厌C++的错误消息...笑^^错误:预期的构造函数,析构函数或类型转换之前“(”令牌
基本上,我下面写着什么在this post尝试为项目符号创建一个工厂类,以便它们可以从一个字符串实例化,这个字符串将从一个xml文件中解析出来,因为我不想为所有类都有一个开关函数,因为这看起来很难看。
这里是我的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
而且我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>));
}
};
在此先感谢。
与错误无关,但有没有办法让Bullet包含BulletFactory而不会产生大量的错误(因为包含循环)?这样我就能从子弹的所有子类的顶部删除#include "BulletFactory.h"
。
我很好奇是什么倒票。这是一个很好的问题。 – GManNickG 2010-04-11 21:04:56