c++ 中带有简单构造函数的向量和自定义类的问题

Issue with Vectors and custom classes in c++ with trivial constructor

本文关键字:向量 问题 自定义 构造函数 简单 c++      更新时间:2023-10-16

我在使用带有我正在创建的自定义类的向量时遇到问题。这是我的代码

vector<image> frames;

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

图像的构造函数只是 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.

确保在复制构造函数中提供引用&

image(const image& other);

看起来,您可能正在传递常量值。

如果image只是一个微不足道的类,请尝试删除所有形式的构造函数;自动生成的构造函数应该可以正常工作。

如果您

使用的是 C++11,我建议您通过 image tempImage{}; 显式调用构造函数。否则,你的循环是无限的,因为你永远不会递增i

或者,您可以尝试显式删除默认构造函数,并让编译器合成一个构造函数,以便您查看会发生什么。

也许你需要实现一个复制构造函数? 即图像(const Image&_other(;

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:注意:候选构造函数不可行:第一个参数 ("常量图像"(将丢失常量限定符图像(图像&IMG(;

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

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