2016-09-23 36 views
0

我有最新的gcc编译器。 gcc(Ubuntu 6.2.0-3ubuntu11〜14.04)6.2.0无法将'(<​​表达式错误>)'从'<大括号封装初始化列表>'转换为std :: unique_ptr <int []>

关于code :: 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

已投票结束为错字,因为问题只是缺少' def_'。 –

+0

我会注意到这一点,欢呼 – TigerCode

回答

2

我敢肯定的错误是在这里:

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

capacity应改为def_capacity,因为从现在开始,您正在使用函数的名称但没有圆括号,这使得编译器在解析初始化器列表时绊倒了。

编辑:是的,它可以很好的编译。有两个错误,第一个是“非法使用非静态成员函数int ArrayClass::capacity() const”。然后,由于编译器无法解析initalizer列表,所以接下来是初始化程序列表。

编辑2:在.cpp文件,在构造函数中定义,capacity出现阴影的ArrayClass::capacity()功能,但我仍然觉得这是更好地避免只是为了清晰起见,这样的碰撞。

+0

是的,编译后你的修复 - 所以如果我理解正确,int数组的分配需要一个int值,我给它一个变量,所以编译器生气了我告诉我,我不能那样做。 – TigerCode

+0

另外'int ArrayClass :: capacity()const'在修复后似乎不是一个错误,所以两只鸟一块石头? (也谢谢你!) – TigerCode

+0

你做了什么来提供一个*函数名*甚至没有调用,因为它没有parens。这本身就是编译器错误,要在此上下文中使用类似的func名称。至于const vs变量,我知道C++ 14很少,但我认为你需要声明'ArrayClass :: capacily()'为'constexpr'才能在这里使用它。 – iksemyonov

相关问题