0

作成中のカスタム クラスでベクトルを使用すると問題が発生します。これが私のコードです

vector<image> frames;


int i = 0;
while (i < n) {
  image tempImage;
  tempImage.read(fullname);
  frames.push_back(tempImage);
}

image のコンストラクタは image() {}; です。

簡単なものが欠けていると確信していますが、それが何であるかわかりません。ここに私のエラーがあります

/usr/include/c++/4.2.1/ext/new_allocator.h:107:20: error: no matching constructor for initialization of 'image'
  { ::new(__p) _Tp(__val); }
               ^   ~~~~~
/usr/include/c++/4.2.1/bits/stl_vector.h:604:20: note: in instantiation of member function
  '__gnu_cxx::new_allocator<image>::construct' requested here
        this->_M_impl.construct(this->_M_impl._M_finish, __x);
                      ^
video.cpp:50:10: note: in instantiation of member function 'std::vector<image, std::allocator<image> >::push_back'
  requested here
            frames.push_back(tempImage);
                   ^
./image.h:25:4: note: candidate constructor not viable: 1st argument ('const image') would lose const qualifier
image(image &img);
^
./image.h:24:4: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
image();
^
In file included from video.cpp:1:
In file included from ./video.h:6:
In file included from /usr/include/c++/4.2.1/vector:73:
/usr/include/c++/4.2.1/bits/vector.tcc:252:8: error: no matching constructor for initialization of 'image'
      _Tp __x_copy = __x;
          ^          ~~~
/usr/include/c++/4.2.1/bits/stl_vector.h:608:4: note: in instantiation of member function 'std::vector<image,
  std::allocator<image> >::_M_insert_aux' requested here
      _M_insert_aux(end(), __x);
      ^
video.cpp:50:10: note: in instantiation of member function 'std::vector<image, std::allocator<image> >::push_back'
  requested here
            frames.push_back(tempImage);
                   ^
./image.h:25:4: note: candidate constructor not viable: 1st argument ('const value_type' (aka 'const image')) would
  lose const qualifier
image(image &img);
^
./image.h:24:4: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
image();
^
./image.h:26:4: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
image(int rows, int columns);
^
2 errors generated.
4

4 に答える 4

3

&コピー コンストラクターで参照を提供していることを確認します。

image(const image& other);

代わりに const 値を渡しているようです。

image単純なクラスの場合は、すべての形式のコンストラクターを削除してみてください。自動生成されたものは正常に動作するはずです。

于 2013-10-10T15:57:49.543 に答える
0

おそらく、コピー コンストラクターを実装する必要がありますか? つまり、Image(const Image& _other);

于 2013-10-10T15:58:33.290 に答える
0

C++11 を使用している場合は、コンストラクターを明示的に呼び出すことをお勧めしimage tempImage{};ます。そうしないと、 while loop はインクリメントしないため、無限ループになりますi

または、デフォルトのコンストラクターを明示的に削除して、コンパイラーにコンストラクターを合成させて、何が起こるかを確認することもできます。

于 2013-10-10T15:56:41.427 に答える
0

imageコピー コンストラクターが必要です。C++11 標準 (セクション 12.8) では、それらがどのように見えるかを定義しています。

class X
{
public:
    X(); // default constructor

    X(const X& x); // const-reference copy constructor
    X(X& x); // non-const copy constructor
    X(volatile X& x); // volatile reference copy constructor
    X(const volatile X& x); // const volatile reference copy constructor
    X(const X& x, int i = 0); // can use any of the above with defaulted values as well
};

コピー コンストラクターではなくムーブ コンストラクターを定義した場合、コピー代入演算子も定義しない限り、暗黙的なコピー コンストラクターは作成されません (コピー代入演算子も暗黙的に作成されません)。

エラーの次の部分を詳しく見ると、次のようになります。

./image.h:25:4: 注: 候補コンストラクターは実行できません: 1 番目の引数 ('const image') は const 修飾子 image(image &img); を失います。

(無効な) コピー コンストラクターを として宣言したようですが、image(const image)それに非 const-reference を渡そうとしています。コピー コンストラクターの宣言を に変更するimage(const image&)と、この問題が解決される可能性があります。

または、コピー コンストラクターとコピー代入演算子を private/protected として宣言した場合、同様のエラーが発生します (つまり、クラスをコピー不可にしました)。オブジェクトXを STL コンテナーに保持するにXは、コピー可能でなければなりません (これが、コンテナーに入れることができなかった理由ですauto_ptr)

于 2013-10-10T15:57:40.950 に答える