-3

重複の可能性:
デフォルト コンストラクターを使用しない c++ オブジェクト配列の初期化

私は構造を持っています:

struct aa 
{
    int a;
    char  b [255];

    aa(int number)
    {
        cout<<"creating structure "<<number<<"\n";

    }

    ~aa()
    {
        cout<<"destroying structure"; 
    }
};

この構造の配列を作成しようとしていますが、うまくいきません:

aa *ss = new aa[3](1);

これにより、コンパイラ エラーが発生します。

どうすればいいのですか?

4

1 に答える 1

0
aa(int number=1)
・・・
aa *ss = new aa[3];

また

template <int N>
struct aa
・・・
aa(int number = N)
・・・
aa<1> *ss = new aa<1>[3];

また

#include <new>
・・・
aa(int number=0)
・・・

int main(){
    aa ss[3];//local value
    for(int i=0;i<3;++i)
        new (ss + i) aa(1);//replacement
于 2012-07-22T14:35:31.717 に答える