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