#include<stdio.h>
#include<stdlib.h>
class CROSS
{
public:
const int x;
const int y;
CROSS(int X, int Y): x(X), y(Y)
{
}
~CROSS() {}
};
CROSS** Generate_Cross_Array(int M, int N)
{
CROSS** cross;
cross = new CROSS*[M];
for(int i=0; i<M; ++i)
{
cross[i] = new CROSS[N];
for(int j=0; j<N; ++j)
{
CROSS cross[i][j](i, j);
printf("%d, %d\n",cross[i][j].x, cross[i][j].y);
}
}
return cross;
}
I am trying to create an 2-dimensional object array and initialize it in the function Generate_Cross_Array(int, int), but g++ told me the following:
In file included from main.cc:3:
cross.h: In function 'CROSS** Generate_Cross_Array(int, int)':
cross.h:23: error: no matching function for call to 'CROSS::CROSS()'
cross.h:10: note: candidates are: CROSS::CROSS(int, int)
cross.h:5: note: CROSS::CROSS(const CROSS&)
cross.h:26: error: variable-sized object 'cross' may not be initialized
Thank anyone who give me any solution.
If you're going to use dynamically allocated arrays, you must provide a default constructor. Something like this is appropriate:
CROSS(int X = 0, int Y = 0): x(X), y(Y)
If this isn't an option, consider using a standard library container instead, ie std::vector
.
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.