2017-10-17 98 views
-1

因此,我的目的是構建一個具有可定義數量的「SingleBuffers」的「Multibuffer」。存儲的類型是免費的,所以我想使用模板出於這個原因。我只是在玩一下。「ISO C++禁止在數組new中初始化」:如何直接使用自己的構造函數將模板對象初始化爲C++中的數組

但我不知道如何直接初始化Multibuffer的構造器中的Singlebuffers。 我不會寫下完整的類,而是用構造函數來顯示問題。在處理模板類時,我在頭文件中定義了這兩個類,因爲它應該完成。

所以我Singlebuffer構造函數如下:

enum BufferState_E 
    { 
    BUFFER_WRITE = 0, 
    BUFFER_READ, 
    BUFFER_FORCED_READ // buffer is forced for reading 
    }; 

    template<typename T> 
    class SingleBuffer 
    { 
    public: 
    /* 
    * Constructor 
    * constructs a Buffer with the concrete data field 
    */ 
    SingleBuffer(unsigned int buffer_size) 
    : entry_ptr_  (new T[buffer_size]()) //default constructor works? 
    , end_entry_ptr_ (&entry_ptr_[buffer_size-1]) 
    { 
     read_entry_ptr_ = &entry_ptr_[0]; 
     write_entry_ptr_ = &entry_ptr_[0]; 
     state_ = BUFFER_WRITE; 
    } 

    private: 
    /* The pointer to the buffer */ 
    T * entry_ptr_; 
    /* the pointers to the entry which is currently read/written */ 
    T *read_entry_ptr_; 
    T *write_entry_ptr_; 
    /* the pointer to the end of the buffer */ 
    T * end_entry_ptr_; 
    /* The state of the buffer */ 
    BufferState_E state_; 
    } 

和我多緩衝構造函數其實例Singlebuffers量(我寫的代碼有問題的行後註釋):

template<typename T> 
    class MultiBuffer : public BufferBase 
    { 
    public: 

    MultiBuffer(unsigned int buffer_size = 2000 // size of one buffer 
       , unsigned char number_of_buffers = 5 // how many buffers are there? 
       , const char* name = "MultiBuffer" 
       ) 
    : buffer_ptr_   (new SingleBuffer<T>[number_of_buffers](buffer_size)) // the problem line! 
    , end_buffer_ptr_  (&buffer_ptr_[number_of_buffers-1]) 
    , buffer_size_  (buffer_size) 
    , number_of_buffers_ (number_of_buffers) 
    { 
     read_buffer_ptr_ = &buffer_ptr_[0]; 
     write_buffer_ptr_ = &buffer_ptr_[0]; 
    } 

    private: 
    /* the normal buffer-pointer */ 
    SingleBuffer<T> *buffer_ptr_; 
    /* the pointer to the end of the buffers */ 
    SingleBuffer<T> *end_buffer_ptr_; 
    /* the pointer to the current read-buffer */ 
    SingleBuffer<T> *read_buffer_ptr_; 
    /* the pointer to the current write-buffer */ 
    SingleBuffer<T> *write_buffer_ptr_; 
    /* the buffer-size */ 
    unsigned int buffer_size_; 
    /* the number of buffers */ 
    unsigned char number_of_buffers_; 
    } 

所以我的編譯器總是告訴我:「標準的代碼行中」ISO C++禁止初始化array new「。查找「新」的cpp參考並不真正有幫助,其他問題與模板無關。 如果我只是使用默認的構造函數,就像在Singlebuffer初始化中看到的那樣,它似乎可以工作,但只要我想將值傳遞給我自己定義的模板類的構造函數,它就會崩潰。我確實設法強制它在沒有直接在Multibuffer-Constructor中初始化的時候進行構建,而是通過一個迭代「緩衝區數量」並在每次迭代中分配內存的循環,但這不是它應該如何工作的,除了我並不真正知道這是否真的沒有錯誤。

而我不想使用std :: - functions,因爲我正在開發嵌入式系統。編譯器是GCC 4.8.1,也不可更改,並且lambda表達式也不受支持...

我希望會有一種可能性。非常感謝。

+4

請思考[mcve]的**最小**和**完整**方面。 – StoryTeller

+3

支持'new'的嵌入式系統應該支持'std :: vector'就好了。 –

+0

好的,我刪除了一個不必要的繼承,並添加了BufferState_E-enum。我希望現在好了。它完全符合我的錯誤。 –

回答

2

錯誤消息說明:您不能在new[]中提供初始化。您將不得不稍後分配給這些對象,可能在MultiBuffer::MultiBuffer的主體中。

但是你不應該那樣做。您只需使用std::vector即可滿足您的動態陣列需求。你的編譯器提供它,庫編寫者知道他們在做什麼。 「標準庫函數臃腫」並非如此本世紀