2014-02-05 26 views
1

我想創建Opencv CvSVM對象的std::vector。當我編譯的代碼:如何創建CvSVM的向量

typedef vector<CvSVM> svm_vec; 

svm_vec svm_data = svm_vec(); 

發生錯誤:

In file included from 2dpca.cpp:5:0: 
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, const _T2&) [with _T1 = CvSVM; _T2 = CvSVM]’: 
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const CvSVM*, std::vector<CvSVM> >; _ForwardIterator = CvSVM*; bool _TrivialValueTypes = false]’ 
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const CvSVM*, std::vector<CvSVM> >; _ForwardIterator = CvSVM*]’ 
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const CvSVM*, std::vector<CvSVM> >; _ForwardIterator = CvSVM*; _Tp = CvSVM]’ 
/usr/include/c++/4.8/bits/stl_vector.h:316:32: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = CvSVM; _Alloc = std::allocator<CvSVM>]’ 
2dpca.cpp:79:29: required from here 
/usr/local/include/opencv2/ml/ml.hpp:553:5: error: ‘CvSVM::CvSVM(const CvSVM&)’ is private 
    CvSVM(const CvSVM&); 
    ^
In file included from /usr/include/c++/4.8/bits/stl_tempbuf.h:60:0, 
       from /usr/include/c++/4.8/bits/stl_algo.h:62, 
       from /usr/include/c++/4.8/algorithm:62, 
       from /usr/local/include/opencv2/core/core.hpp:56, 
       from 2dpca.cpp:1: 
/usr/include/c++/4.8/bits/stl_construct.h:83:7: error: within this context 
     ::new(static_cast<void*>(__p)) _T1(__value); 

編譯器:克++ 4.8 OpenCV的版本2.4.8

+1

'的std :: VECTOR'需要對象它管理爲[拷貝構造](http://en.cppreference.com/w/cpp/concept/CopyConstructible)。由於'CsSVM​​'的拷貝構造函數是私有的,它不符合這個要求。 –

+0

我明白這一點。有兩個問題: 1)爲什麼要引入這個要求? 2)在這種情況下,我需要什麼類型的容器除了通常的陣列 – iGriffer

+1

每個容器都有一組基於其功能的要求。當矢量的大小發生變化時,將分配一個新的內存塊並將這些值複製到其新位置。你將需要選擇一個容器,它不需要它保持可複製構造的值(因爲'CvSVM'沒有移動構造函數)或使用指針 - 'std :: vector >'。 –

回答

2

由於CvSVM(又名SVM)是不可拷貝,則需要在你的向量中存儲指向它的指針。你可以使用OpenCV智能指針cv :: Ptr <>來做到這一點。請記住使用操作員 - >然後訪問SVM的方法。

這是一個工作。

這解決了這個問題。

#include <opencv2\opencv.hpp> 
#include <vector> 
using namespace std; 
using namespace cv; 

int main() 
{ 
    vector<Ptr<SVM>> svm_bank; 

    for (int i = 0; i < 3; ++i) 
    { 
     Mat trainData; 
     Mat responses; 

     /*Code for trainingData and 
     responses */ 

     SVM *new_model; 
     new_model = new SVM; 
     new_model->train(trainData, responses); 

     svm_bank.push_back(new_model); 
    } 

    for (int i = 0; i < 3; ++i) 
    { 
     Mat samples; 
     Mat results; 
     svm_bank[i]->predict(samples, results); 
    } 

    return 0; 
}