如何在cpp中用线程初始化对象?这可行吗



我想初始化两个Collage对象,用线程创建它们是否可行?如果可行,如何?我想创建两个拼贴画,然后在不同的线程中分别调用2-stitch,最后将它们修改后的图像组合到一个新的图像向量中,形成一个新拼贴画。这是我拼贴的.h文件


#include <iostream>
#include <stdarg.h>
namespace img {
class Collage {
public:
Collage( vector<Image> imageArr);
~ Collage();
//create an array of coordinates of corners of each of the images, variable
//                int subImageCorners[numImages * 4];
void twoStitch(bool );
void threeStitch();
void fourStitch(bool );
void fourStitchRec(int times);
void flip();
const std::vector<int>& testVect();
int getNumImages();
const std::vector<double>& getRatios();
const std::vector<Image>& getImageArr();
Mat getModifiedImage();
void setModifiedImageArr(vector<Image> imageArrModified);
vector<Image> getModifiedImageArr();
void setModifiedImage(Mat modifiedMat);
const std::vector<double>& getModifiedRatios();
void setFourStitchRecImgArr(Mat image);
const vector<Image>& getFourStitchRecImgArr();
private:
vector <Image> fourStitchRecImgArr;
int numImages;
int modifiedNumImages;
vector <double> ratios;
vector <double> modifiedRatios;
vector <Image> imageArr;
Mat modifiedImage;
vector <Image> imageArrModified;
void fourStitchRecAux(bool, int times);
};
}

我试着只调用线程中的成员函数如下:

if (this->getNumImages() == 4) {
vector<double> ratios;
vector<Image> subImageArr1;
if (original){
ratios = this-> getRatios();
subImageArr1 = this->getImageArr();
} else{
ratios = this-> getModifiedRatios();
subImageArr1 = this->getModifiedImageArr();
}
int maximum = getMaxIndex(ratios);
// std::cout << "maximum: " << typeid(maximum).name();
vector<Image> subImageArr2;
//split into two collages of double stitch and then stitch all of them together
vector<Image>::iterator maxIndex = subImageArr1.begin() + maximum ;
vector<double>::iterator maxRatiosIndex = ratios.begin() + maximum ;
subImageArr2.push_back(*maxIndex);
ratios.erase(maxRatiosIndex);
subImageArr1.erase(maxIndex);
int secondMaximum = getMaxIndex(ratios);
vector<Image>::iterator secondMaxIndex = subImageArr1.begin() + secondMaximum ;
subImageArr1.erase(secondMaxIndex);
subImageArr2.push_back(*secondMaxIndex);
Collage subCollage1(subImageArr1);
Collage subCollage2(subImageArr2);
std::thread th1(&Collage::twoStitch, subCollage1);
std::thread th2(&Collage::twoStitch, subCollage2);
th1.join();
th2.join();
vector<Image> imageArrModified ={subCollage1.getModifiedImage(), subCollage2.getModifiedImage()};
this->setModifiedImageArr(imageArrModified);
this->twoStitch(false);

} else{
std::cout << "A different amount of images than 4!";
}
}```
And I get the errors bellow:
> /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/thread:280:5: error: attempt to use a deleted function
>    __invoke(_VSTD::move(_VSTD::get<1>(__t)), >_VSTD::move(_VSTD::get<_Indices>(__t))...);
>    ^
>/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/thread:291:5: note: in instantiation of function template >specialization >'std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_s>truct, void (img::Collage::*)(bool), img::Collage, 2>' requested >here
>    __thread_execute(*__p, _Index());
>    ^
>/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/thread:307:47: note: in instantiation of function template >specialization >'std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::_>_1::__thread_struct>, void (img::Collage::*)(bool), img::Collage>>' >requested here
>    int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, >__p.get());
>                                              ^
>/Users/yao/CLionProjects/video_editor_BX23/src/image/test_image_class >/../collage/collage.cpp:199:21: note: in instantiation of function >template specialization 'std::__1::thread::thread<void >(img::Collage::*)(bool), img::Collage &, void>' requested here
>        std::thread th1(&Collage::twoStitch, subCollage1);
>                    ^
>/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include >/c++/v1/type_traits:1967:5: note: '~__nat' has been explicitly marked >deleted here
>    ~__nat() = delete;
>    ^

看起来您正试图在实例subCollage1subCollage2上调用成员函数Collage::twoStitch,但Collage::twoStitch需要一个您没有提供的bool参数。此外,要调用成员函数,需要提供一个指向实例的指针。

示例:

#include <iostream>
#include <thread>
struct Collage {
void twoStitch(bool b) {
std::cout << b << 'n';
}
};
int main() {
Collage subCollage1;

// 1. pointer to member function
// 2. pointer to instance
// 3. the argument(s) the function requires
std::thread th1(&Collage::twoStitch, &subCollage1, true);

th1.join();
}

演示

最新更新