I have the latest gcc compiler. gcc (Ubuntu 6.2.0-3ubuntu11~14.04) 6.2.0
On code::blocks I've set the compiler to the GNU Compiler as default.
The error I'm getting is this
error: could not convert
{<expression error>}
from<brace-enclosed initializer list>
tostd::unique_ptr<int []>
The issue is in my header file at the bottom.
This was for a lab that I did in class and It worked fine last week I think - compiling the header file gives me my results but when i build from the arrayclass.cpp file it gives me this error.
Here is my implementation file 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;
}
And here is my header file:
#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
I'm pretty sure the error is here:
unique_ptr<int[]> arr {make_unique<int[]>(capacity)};
capacity
should be def_capacity
instead, since as of now, you're using the name of a function but without the round parentheses, which makes the compiler stumble while parsing the {}
initializer list.
Edit : Yes, it compiles fine with the fix. There are two errors, the first one being " invalid use of non-static member function int ArrayClass::capacity() const
". Then the initializer-list one follows, since the compiler can't parse the initalizer list.
Edit 2 : In the .cpp
file, in the constructor definition, capacity
appears to shadow the ArrayClass::capacity()
function, but I still think it'd be better to avoid such a collision just for clarity sake.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.