我想创建一个Opencv CvSVM对象的std::vector。当我编译这段代码时: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);编译器:g++ 4.8OpenCV版本2.4.8 最佳答案 由于CvSVM(又名SVM)不可复制,因此您需要将指向它的指针存储在 vector 中。您可以使用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;}
08-17 04:28