2016-05-31 30 views
0

時的循環性我想這樣做一個類,其中包含NBT格式指定的任意值。它是一種json,但更先進。使用基數和派生類作爲數組在

因此,我創建了一個包含ListValue(無名稱的值)和另一個包含Value(帶名稱)的類。在派生類,我擡離基部

using ListValue::operator=; 

在第三文件=操作我有兩個usings:

using CompoundData = std::vector<Value>; 
using ListData = std::vector<ListValue>; 

ListValue具有私有成員爲:

union ValueHolder 
{ 
    Byte vByte; 
    Short vShort; 
    Int vInt; 
    Long vLong; 
    Float vFloat; 
    Double vDouble; 
    String* pString; 
    CompoundData* pCompound; 
} mData; 

(I以後會添加ListData *)

問題是我完全不知道如何包含所有這些標頭s o它將與循環一起工作。我嘗試了幾個前向聲明和容器作爲向量或向量,並帶有智能指針來打破它,但是沒有爲我工作。

如果您能幫助我爲我的代碼提供一個想法/解決方案,我將非常感激。非常感謝你。

+0

你應該嘗試創建一個重現你的問題的最小例子。編譯器消息通常是一個很好的起點。 –

+0

從設計上的錯誤幾乎總是出現循環性......將設計更改爲沒有循環性,將來會爲您節省很多麻煩。你有很多選擇來實現這一點,首先我想到的是多態性 – Garf365

+0

什麼是NBT格式?另外,爲什麼你的類型會受到欺騙?一個錯字? –

回答

0

簡單的方法是:

struct CompoundData; 
struct ListData; 

然後在別處:

struct CompoundData : std::vector<Value> { 
    using std::vector<Value>::vector; 
    CompoundData& operator=(std::vector<Value> const& o) { 
    static_cast<std::vector<Value>&>(*this)=o; 
    return *this; 
    } 
    CompoundData& operator=(std::vector<Value> && o) { 
    static_cast<std::vector<Value>&>(*this)=std::move(o); 
    return *this; 
    } 
    CompoundData(std::vector<Value> const& o): 
    std::vector<Value>(o) 
    {} 
    CompoundData(std::vector<Value> && o): 
    std::vector<Value>(std::move(o)) 
    {} 
    CompoundData()=default; 
    CompoundData(CompoundData const&)=default; 
    CompoundData(CompoundData &&)=default; 
    CompoundData& operator=(CompoundData const&)=default; 
    CompoundData& operator=(CompoundData &&)=default; 
}; 

這是說:「不,我並不比std::vector我繼承真不同」一幫樣板,和ListData一樣。