2

我正在尝试创建一个二维对象数组。我正在尝试将我创建的类的构造函数设置为将接受参数的构造函数。但是,我不断收到错误消息:

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 = 0; j < 9; j++)
    {
      cin >> s[i][j];
      arr[9][9] = new test(s[i][j]);
    }
  }
  return 0;
}

以测试为类。谁能帮我找出这个错误?我知道新函数返回一个 void 指针,但我怎样才能得到它,以便我的 2d 数组接受参数?

谢谢。

4

4 回答 4

2

您的二维数组不是指向test对象的指针数组。无论如何,内存是为您的二维数组分配的。所以不需要循环。

除非你改变你的声明

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]);
      }
     }
于 2012-10-13T22:12:39.757 回答
0

//具有一个 arg 构造函数的类/无默认构造函数,其 2d 数组需要 // 使用 new 在堆上分配

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;
}
于 2013-07-31T09:21:08.500 回答
0

在您的代码中,您基本上存在类型不匹配,因为您尝试将指向 X 的指针分配给 X。我假设会发生这种情况,因为您尝试分配 a 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);
  }
}
于 2013-08-06T07:41:18.620 回答
0

当您声明数组时,test arr[9][9]; 分配内存并为每个成员调用默认构造函数。所以你不需要调用new来分配新的内存。我假设您的目标是test使用从std::cin.

然后你有几个选择:

  • 最简单的解决方案是使用二维指针数组:

它看起来像:

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]);
  }
}
  • 如果要保留普通test对象数组(没有指针),则可以使用:

提供一种test::set(int)方法来设置构造后的值。

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].set(s[i][j]);
  }
}

operator=(const test&)临时构造对象,然后使用(或operator=(test &&)在 c++11 中)将其分配给数组中已分配的对象。请注意,这里没有new

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]);
  }
}

或者使用placement new(这会在预分配的内存块中构造新对象):

test arr[9][9];
for(unsigned int i = 0; i < 9; i++)
{
  for(unsigned int j = 0; j < 9; j++)
  {
     cin >> s[i][j];
     new(&arr[i][j]) test(s[i][j]);
  }
}
  • 最后一个:如果您没有特定的理由使用静态二维数组,请使用一些 STL 容器。
于 2012-10-13T23:14:38.393 回答