1

How do I initialize an array through a constructor? Here is the code for a class called Sort:

private:
    double unpartitionedList[]; 

public:
    Sort(double unpartitionedList[]);

Sort::Sort(double unpartitionedList[])
{
    this->unpartitionedList = unpartitionedList;
}

I'd like to be able to pass an array to the constructor and have it stored in unpartitionedList[]. Like this: Sort(array[])

As the code is now, I get a compiler error in DevC++:

"[Error] incompatible types in assignment of 'double*' to 'double[0]'"

I've tried inserting reference (&) and dereference (*) operators where I thought they were needed, but unfortunately, my attempts were unsuccessful.

Any suggestions would be greatly appreciated.

2
  • 2
    This is not legal. You must give the array an extent. I suggest you use std::array or std::vector, though. Commented Nov 3, 2013 at 22:55
  • Class member variables must have complete type. Commented Nov 3, 2013 at 22:59

2 Answers 2

4

Arrays aren't assignable. You'll have to do an element-wise copy or write actual C++ code and use std::array or std::vector.

Sign up to request clarification or add additional context in comments.

2 Comments

You can initialise arrays using { … } syntax in an initialisation list. No need to assign to them. That said, std::vector is a better idea regardless, probably with a template constructor that takes an iterator pair.
As you say vector is likely the way to go, but we're covering arrays in class and the instructor may like us to use the ordinary, run-of-the-mill arrays. That being said What do you mean by "You can initialise arrays using { … } syntax in an initialisation list."
2
class Sort
{
private:
    double unpartitionedList[]; 

public:
    Sort(double unpartitionedList[]);
};

Sort::Sort(double unpartitionedList[])
{
    this->unpartitionedList = unpartitionedList;
}

That code will not compile as arrays are not assignable. You can accomplish your goal a few different ways (depending on the requirements you haven't mentioned).

Method 1: Manual Memory Management

class Sort
{
private:
    double* unpartitionedList;
    std::size_t _size; 

public:
    Sort(double* upl, std::size_t n);
};

Sort::Sort(double* upl, std::size_t n) : unpartitionedList(upl), _size(n)
{

}

There are a few things to note here: If you intend for this class to take ownership of the memory, you will have to properly manage it (e.g. free the memory in the destructor, and provide a proper copy-constructor that will perform a deep-copy). Because of the memory management requirements, this method is not recommended if not absolutely necessary.

Method 2/3: STD Containers

class Sort
{
private:
    std::vector<double> _upl;
    // or 
    // std::array<double, SIZE> upl; // with a constant SIZE defined

public:
    Sort(const std::vector<double>& upl);
};

Sort::Sort(const std::vector<double>& upl) : _upl(upl)
// Sort::Sort(const std::array<double, SIZE>& upl) : _upl(upl)
{

}

This will remove the memory management requirement. std::vector will allow you to size the array at runtime. std::array is a thin wrapper around a C-style array (and must be sized at compile time).

2 Comments

Since we are covering arrays in class, I think the instructor would prefer the use of regular old arrays over std::vector or std::array.
@navig8tr You will find that what your professors prefer and what is considered good practice professionally will often be two very different things. That said, if he is expecting you to deal with arrays as members of your class, you will either need to define a constant size for your member array, or use dynamic memory (which is done in method 1).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.