使用简单的构造函数在c ++中使用向量和自定义类的问题

时间:2013-10-10 15:52:53

标签: c++ c templates vector

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

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.

4 个答案:

答案 0 :(得分:3)

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

image(const image& other);

看起来,您可能会通过const值传递。

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

答案 1 :(得分:0)

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

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

答案 2 :(得分: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
};

如果您定义了一个move-constructor而不是一个copy-constructor,则不会为您创建隐式复制构造函数(也不会隐式创建复制赋值运算符),除非您将复制赋值运算符定义为好。

如果您仔细查看错误的以下部分:

  

./ image.h:25:4:注意:候选构造函数不可行:第一个参数   ('const image')会丢失const限定符图像(image&amp; img);

您似乎已将(无效)复制构造函数声明为image(const image),但正在尝试向其传递nonconst-reference。将您的复制构造函数声明更改为image(const image&),这可能会解决此问题。

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

答案 3 :(得分:0)

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