2013-10-10 30 views
0

我有一個使用向量與我創建的自定義類的問題。這裏是我的代碼問題與向量和自定義類在c + +微不足道的構造函數

vector<image> frames; 


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

而圖像的構造函數只是圖像(){};

我相信我失去了一些東西簡單,但我無法弄清楚它是什麼。這裏是我的錯誤

/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. 
+0

必須是圖像類中的拼寫錯誤。你能否分享它的聲明? –

回答

3

確保你在你的拷貝構造函數提供參考&

image(const image& other); 

看來,你可能會通過const值傳遞。

如果image只是一個普通的類,請嘗試刪除所有形式的構造函數;自動生成的應該可以正常工作。

+1

實際上,沒有(我以前也一樣):複製構造函數是用非''const'引用定義的。 –

+0

@KonradRudolph隱式的將被創建爲一個非const引用,但可以將它作爲一個const引用(但不是由值)。 C++ 11,12.8.2 –

+0

@Zac「這是可以接受的」 - 是的,但不在OP的用例中。並且錯誤消息明確說明這是這裏的原因。 '__gnu_cxx :: new_allocator ::構造'嘗試並且無法調用'T(T const&)'。 –

0

如果您使用C++ 11,我建議通過image tempImage{};顯式調用構造函數。否則,你是while循環是無限的,因爲你永遠不會增加i

或者,您可以嘗試刪除顯式的默認構造函數,並讓編譯器綜合一個給你看看會發生什麼。

+0

'image tempImage {};''和'image tempImage;'對於用戶定義的類型是相同的。 – juanchopanza

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: note: candidate constructor not viable: 1st argument ('const image') would lose const qualifier image(image &img);

您似乎宣佈了(無效)拷貝構造函數爲image(const image),而是嘗試將非const引用傳遞給它。將您的複製構造函數聲明更改爲image(const image&),並且可能會解決此問題。

或者,如果您將複製構造函數和複製賦值運算符聲明爲private/protected,您將得到類似的錯誤(即,使您的類不可複製)。爲了保持任何對象X在STL容器,X必須可拷貝(這就是爲什麼你不能把auto_ptr在一個容器)

+0

第一個位聽起來不對:定義一個(非複製)構造函數不會影響編譯器生成的構造函數。 – juanchopanza

+0

@juanchopanza在午餐前發表,並沒有傳達我想要的東西......我會解決它。 –

0

也許你需要實現一個複製構造函數?即圖像(const Image & _other);