2016-09-23 13 views
0

私は最新のgccコンパイラを持っています。 gcc(Ubuntu 6.2.0-3ubuntu11〜14.04)6.2.0は '<brace-enclosed initializer list>'から 'std :: unique_ptr'に '(<式エラー>)'を変換できませんでした。<int []>

コード:blocks私はコンパイラをデフォルトとしてGNUコンパイラに設定しました。

私は取得していますエラーは、問題が一番下に私のヘッダファイルにあるこの

error: could not convert {<expression error>} from <brace-enclosed initializer list> to std::unique_ptr<int []>

です。

これは私がクラスで行ったラボのためのもので、先週はうまくいきました。ヘッダーファイルをコンパイルすると結果は得られますが、arrayclass.cppファイルからビルドするとこのエラーが出ます。ここで

は私の実装ファイルはArrayClass.cppです:

#include "ArrayClass.h" 

#include <memory> 

using std::make_unique; 

ArrayClass::ArrayClass(int capacity) 
    : arrSize{capacity}, 
    arr{make_unique<int[]>(capacity)} 
{ 
} 

void ArrayClass::insert(int value) 
{ 
    if (currentSize < arrSize) { 
     arr[currentSize++] = value; 
    } 
} 

void ArrayClass::set(int i, int value) 
{ 
    if (i >= 0 && i < currentSize) { 
     arr[i] = value; 
    } 
} 

int ArrayClass::get(int i) const 
{ 
    if (i >= 0 && i < currentSize) { 
     return arr[i]; 
    } 
    else { 
     return 0; 
    } 
} 

int ArrayClass::capacity() const 
{ 
    return arrSize; 
} 

int ArrayClass::size() const 
{ 
    return currentSize; 
} 

そして、ここでは私のヘッダファイルです:

#ifndef ArrayClass_header 
#define ArrayClass_header 
#include <memory> 

using std::unique_ptr; 
using std::make_unique; 

class ArrayClass 
{ 
public: 
    // Constructors and Destructors 

    // Default constructor 
    // POST: Created an ArrayClass object with an array of size def_size 
    // Compiler default suffices (see variable initializations at end of header) 
    ArrayClass() = default; 

    // Constructor 
    // PRE: capacity > 0 
    // POST: Created an ArrayClass object with an array of size capacity 
    // PARAM: capacity = size of the array to allocate 
    ArrayClass(int capacity); 

    // Destructor 
    // POST: All dynamic memory associated with object de-allocated 
    // ~ArrayClass(); 

    // Set the value of the next free element 
    // PRE: currentSize < arraySize 
    // POST: Element at index currentSize set to value 
    // PARAM: value = value to be set 
    void insert(int value); 

    // Return an element's value 
    // PRE: 0 <= i < arraySize 
    // POST: Returned the value at index i 
    // PARAM: i = index of value to be returned 
    int get(int i) const; 

    // Set an element's value 
    // PRE: 0 <= i < arraySize 
    // POST: Element at index i set to value 
    // PARAM: i = index of element to be changed 
    //  value = value to be set 
    void set(int i, int value); 

    // POST: Return the currently allocated space 
    int capacity() const; 

    // POST: Return the number of elements 
    int size() const; 

    // The default capacity 
    static constexpr int def_capacity {10}; 

private: 
    int arrSize {def_capacity}; 
    int currentSize {0}; 
    unique_ptr<int[]> arr {make_unique<int[]>(capacity)}; 
}; 

#endif 
+0

誰かがあなたのヘッダーを使いたいが、 '使用している 'ものを持っていない場合には、' using'宣言をヘッダに入れない方がいいスタイルです。 –

+0

typoとして閉じることに投票しました。 def_'。 –

+0

私もそれに注意してください。 – TigerCode

答えて

2

私はエラーがここにあるかなり確信している:

unique_ptr<int[]> arr {make_unique<int[]>(capacity)}; 

capacityは、代わりにdef_capacityにする必要があります。今のところfunctiという名前を使用していますラウンドカッコを使用していますが、コンパイラは{}イニシャライザリストを解析している間、スタンドアルになります。

を編集します。はい、問題なく修正されます。 2つのエラーがあります。最初は "非静的メンバー関数int ArrayClass::capacity() const"の無効な使用です。コンパイラが初期化子リストを解析できないため、初期化子リストが続きます。

編集2は:.cppファイルでは、コンストラクタの定義では、capacityArrayClass::capacity()機能をシャドウに見えますが、私はまだそれだけで、明確にするために、このような衝突を回避する方が良いと思います。

+0

それはあなたの修正の後にコンパイルされます。つまり、正しく理解すれば、int配列の割り当てにはint値が必要で、代わりに変数を与えてコンパイラが怒ります私はそれをすることができないと私に言った。 – TigerCode

+0

また、 'int ArrayClass :: capacity()const'は修正後のエラーではないようですので、1つの石で2つの鳥? (またありがとう!) – TigerCode

+0

括弧のないものだったので、*関数名*には何もしませんでした。それ自体がコンパイラエラーです。この文脈では、そのようなfunc名を使用してください。 const vs変数まではC++ 14はほとんど分かりませんが、ここではArrayClass :: capacily()を 'constexpr'として宣言する必要があると思います。 – iksemyonov

関連する問題