Re: Initializing arrays of objects
On Oct 20, 6:31 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Cristiano wrote:
I'm trying to compile this code:
template <typename T> class matrix
{
private:
class row
{
public:
T *data;
row(int n) {data = new T[n];};
...
} *rdata;
int rows, cols;
public:
matrix(int r, int c) { rows = r; cols = c; rdata = new row[r](=
c); };
...
but I get:
'Target of operator new()' : array initialization needs curly b=
races
and
'new' : 'matrix<T>::row' no default constructor to initialize a=
rrays of
objects.
Any way to fix the problem?
The simpler example would be
struct A {
A(int); // whatever that thing does
};
int main() {
A* arrA = new A[42](42); // I want a dynamic array of 42=
'A'
// object=
s and each initialised with
// the nu=
mber 42...
}
And, unfortunately, there is no solution, AFAIK.
If this is not just an exercise, consider using existing designs for
matrices. If this is for learning, consider that you've learned
something (the impossibility of initializing individual elements of a
dynamic array), and move on.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I think it actually might be the gslices and valarrays.
http://www.cplusplus.com/reference/std/valarray/gslice/
http://www.cplusplus.com/reference/std/valarray/valarray/operator%5B%5D/
I think it may be in C++ much of the function.
Regards,
Ross F.