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

如果您使用的是 C++11,我建议您通过image tempImage{};. 否则,你的 while 循环是一个无限循环,因为你永远不会递增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: 注意:候选构造函数不可行:第一个参数 ('const image') 将丢失 const 限定符 image(image &img);

看来您已将(无效的)复制构造函数声明为image(const image),但正试图向它传递非常量引用。将您的复制构造函数声明更改为image(const image&),这可能会解决此问题。

或者,如果您将复制构造函数和复制赋值运算符声明为私有/受保护,您将得到类似的错误(也就是说,您使您的类不可复制)。为了X在 STL 容器中保存任何对象,X必须是可复制的(这就是您不能放入auto_ptr容器中的原因)

于 2013-10-10T15:57:40.950 回答
0

也许您需要实现一个复制构造函数?即图像(常量图像& _other);

于 2013-10-10T15:58:33.290 回答