C++ 使用带参数的构造函数创建二维对象数组
我正在尝试创建一个二维对象数组。我试图将我创建的类的构造函数设置为将接受参数的构造函数。然而,我不断收到一个错误,说: main.cpp:18:37:错误:从“test*”到“const test&”的用户定义转换无效C++ 使用带参数的构造函数创建二维对象数组,c++,C++,我正在尝试创建一个二维对象数组。我试图将我创建的类的构造函数设置为将接受参数的构造函数。然而,我不断收到一个错误,说: main.cpp:18:37:错误:从“test*”到“const test&”的用户定义转换无效 int main() { test arr[9][9]; char s[9][9]; int integerArray[9][9]; for(unsigned int i = 0; i < 9; i++) { for(unsigned int j
int main()
{
test arr[9][9];
char s[9][9];
int integerArray[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[9][9] = new test(s[i][j]);
}
}
return 0;
}
intmain()
{
测试arr[9][9];
chars[9][9];
整数数组[9][9];
for(无符号整数i=0;i<9;i++)
{
for(无符号整数j=0;j<9;j++)
{
cin>>s[i][j];
arr[9][9]=新测试(s[i][j]);
}
}
返回0;
}
考试是一门课。有人能帮我找出这个错误吗?我知道新函数返回一个空指针,但我如何才能得到它,以便我的2d数组接受参数
谢谢。您的2d数组不是指向
测试对象的指针数组。
内存已分配给2d阵列。所以不需要循环
除非你改变你的声明
test arr[9][9];
例如:
test*** arr = new (test**)[rows];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[i][j] = new test(s[i][j]);
}
}
test***arr=new(test**)[行];
for(无符号整数i=0;i<9;i++)
{
for(无符号整数j=0;j<9;j++)
{
cin>>s[i][j];
arr[i][j]=新测试(s[i][j]);
}
}
当您将数组声明为test arr[9][9]时代码>
然后分配内存,并为每个成员调用默认构造函数。因此,您不需要调用new
来分配新内存。
我假设您的目标是使用从std::cin
读取的值构造test
对象数组
然后您有几个选项:
- 最简单的解决方案是使用2D指针数组:
它看起来像:
test* arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[i][j] = new test(s[i][j]);
}
}
构造临时对象,然后使用操作符=(const test&)
(或c++11中的操作符=(test&)
)将其分配给数组中已分配的对象。请注意,此处没有新的
:
test arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
for(unsigned int j = 0; j < 9; j++)
{
cin >> s[i][j];
arr[i][j] = test(s[i][j]);
}
}
测试arr[9][9];
for(无符号整数i=0;i<9;i++)
{
for(无符号整数j=0;j<9;j++)
{
cin>>s[i][j];
arr[i][j]=试验(s[i][j]);
}
}
或者使用placement new(这将在预先分配的内存块中构造新对象):
测试arr[9][9];
for(无符号整数i=0;i<9;i++)
{
for(无符号整数j=0;j<9;j++)
{
cin>>s[i][j];
新的(&arr[i][j])测试(s[i][j]);
}
}
- 最后一点:如果您没有使用静态2D数组的具体原因,请选择一些STL容器
//具有一个参数构造函数的类/没有需要设置其2d数组的默认构造函数
//使用新的
class A
{
public:
A(int i):m_i(i)
{
cout<<"constructor called i="<<i<<endl;
}
~A()
{
cout<<"destructor called i="<<m_i<<endl;
}
int get()
{
return m_i;
}
friend ostream& operator<<(ostream& os, const A& obj);
private:
int m_i;
};
ostream& operator<<(ostream& os, const A& obj)
{
os<<obj.m_i;
return os;
}
typedef unsigned char Byte;
int main()
{
int m=4;
int n=3;
int s[4][3] = { {1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}
};
//Allocate a buffer to hold the array, on heap
Byte* pBuff = new Byte[sizeof(A)*m*n];
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
A* p = new (pBuff+sizeof(A)*(n*i+j)) A(s[i][j]);
}
}
//Accessing array
A (*testArr)[3] = (A (*)[3])pBuff;
for(int i=0;i<4;++i)
{
for(int j=0;j<3;++j)
{
cout<<"testArr["<<i<<"]["<<j<<"]="<<testArr[i][j]<<endl;
}
}
//after you are done with the array of objects.
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
A* p=(A*)(pBuff+sizeof(A)*(i*n+j));
p->~A(); //call destructor for each object.
}
}
delete[] pBuff; //Delete the buffer.
return 0;
}
A类
{
公众:
A(int i):m_i(i)
{
cout在您的代码中,您基本上存在类型不匹配,因为您试图将指向X的指针分配给X。
我假设会发生这种情况,因为您试图分配一个test
,但是test
没有默认构造函数
std::vector<std::vector<test, test(char(0))> > arr;
char a(0);
for (unsigned int i = 0; i < 9; ++i) {
for (unsigned int j = 0; j < 9; ++j) {
std::cin >> a;
arr[i][j] = test(a);
}
}
一个更简单的解决方案是使用一个向量和一个给定的构造函数,它避免了默认构造函数,但在复制构造方面有一些开销
std::vector<std::vector<test, test(char(0))> > arr;
char a(0);
for (unsigned int i = 0; i < 9; ++i) {
for (unsigned int j = 0; j < 9; ++j) {
std::cin >> a;
arr[i][j] = test(a);
}
}
std::向量arr;
字符a(0);
for(无符号整数i=0;i<9;++i){
for(无符号整数j=0;j<9;++j){
标准:cin>>a;
arr[i][j]=试验(a);
}
}
std::vector<std::vector<test, test(char(0))> > arr;
char a(0);
for (unsigned int i = 0; i < 9; ++i) {
for (unsigned int j = 0; j < 9; ++j) {
std::cin >> a;
arr[i][j] = test(a);
}
}