[英]error: no matching function for call to ‘constructor’ note: candidates are:
#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: 我试图创建一个二维对象数组,并在函数Generate_Cross_Array(int,int)中对其进行初始化,但是g ++告诉我以下内容:
In file included from main.cc:3:
在main.cc:3包含的文件中:
cross.h: In function 'CROSS** Generate_Cross_Array(int, int)':
cross.h:在函数'CROSS ** Generate_Cross_Array(int,int)'中:
cross.h:23: error: no matching function for call to 'CROSS::CROSS()'
cross.h:23:错误:没有匹配的函数可用于调用“ CROSS :: CROSS()”
cross.h:10: note: candidates are: CROSS::CROSS(int, int)
cross.h:10:注意:候选者为:CROSS :: CROSS(int,int)
cross.h:5: note: CROSS::CROSS(const CROSS&)
cross.h:5:注意:CROSS :: CROSS(const CROSS&)
cross.h:26: error: variable-sized object 'cross' may not be initialized
cross.h:26:错误:可变大小的对象“ cross”可能未初始化
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
. 如果不是这样,请考虑使用标准库容器,即
std::vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.