![](/img/trans.png)
[英]Error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'struct'
[英]could not convert '(<expression error>)' from '<brace-enclosed initializer list>' to std::unique_ptr<int []>
我有最新的gcc编译器。 gcc(Ubuntu 6.2.0-3ubuntu11〜14.04)6.2.0
在code :: blocks上,我已将编译器默认设置为GNU编译器。
我得到的错误是
错误:无法将
{<expression error>}
从<brace-enclosed initializer list>
为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
我很确定错误在这里:
unique_ptr<int[]> arr {make_unique<int[]>(capacity)};
capacity
应该改为def_capacity
,因为到目前为止,您使用的是函数名称,但没有圆括号,这会使编译器在解析{}
初始化程序列表时遇到问题。
编辑 :是的,它与修复程序编译正常。 有两个错误,第一个错误是“ 无效使用非静态成员函数 int ArrayClass::capacity() const
”。 然后是初始化列表,因为编译器无法解析初始化列表。
编辑2 :在.cpp
文件中,在构造函数定义中, capacity
似乎ArrayClass::capacity()
了ArrayClass::capacity()
函数,但我仍然认为最好还是为了避免这种冲突而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.