如何解决vector.push_back在推送线程时编译期间会产生已删除的函数错误
我不明白为什么,但是vector<thread>.push_back()
会产生错误。
我尝试用.emplace_back()
替换产生错误的行,但仍然出现此错误。
总的来说,我的代码试图显示基于输入的素数。我肯定知道并且已经测试过,splitString()和isPrime()函数可以正常工作,只是我对main()的实现是不正确的,并且在编译时会产生错误。
这是我的代码;
#include <iostream>
#include <string>
#include <cstdlib>
#include <thread>
#include <vector>
using namespace std;
bool isPrime(int number) {
int scan = number - 1;
while(scan > 1){
if(number % scan == 0){
return false;
}
scan = scan - 1;
}
return true;
}
vector<string> splitString(string String,string seperator){
vector<string> result;
size_t pos = 0;
string token;
while((pos = String.find(seperator)) != string::npos){
token = String.substr(0,pos);
result.push_back(token);
String.erase(0,pos + seperator.length());
}
result.push_back(String);
return result;
}
string getPrimes(string param){
vector<string> splitstring = splitString(param,",");
string outstring = "";
int currentScan = stoi(splitstring[0]);
int higher = stoi(splitstring[1]);
while(currentScan <= higher){
bool res = isPrime(currentScan);
if(res == true){
outstring = outstring + to_string(currentScan) + ",";
cout << to_string(currentScan) + ",";
}
currentScan = currentScan + 1;
}
return outstring;
}
int main(int argc,char ** argv) {
int initVal = atoi(argv[0]);
int threadLength = atoi(argv[1]);
int threadCount = atoi(argv[2]);
vector<thread> threads;
int threadPart = 1;
while(threadPart <= threadCount){
int threadStart = (threadPart * threadLength) + initVal;
int threadEnd = threadStart + threadLength;
string inp = to_string(threadStart) + "," + to_string(threadEnd);
thread th(getPrimes,inp);
threads.push_back(th);
threadPart = threadPart + 1;
}
for(int i = 0; i < threads.size(); i++){
threads[i].join();
}
}
这是产生的错误;
In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/bits/c++allocator.h:33,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/allocator.h:46,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:41,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/locale_classes.h:40,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/ios_base.h:41,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:42,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,from primes.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*,_Args&& ...) [with _Up = std::thread; _Args = {std::thread&}; _Tp = std::thread]':
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/alloc_traits.h:475:4: required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&,_Up*,_Args&& ...) [with _Up = std::thread; _Args = {std::thread&}; _Tp = std::thread; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::thread>]'
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/vector.tcc:103:30: required from 'void std::vector<_Tp,_Alloc>::emplace_back(_Args&& ...) [with _Args = {std::thread&}; _Tp = std::thread; _Alloc = std::allocator<std::thread>]'
primes.cpp:55:32: required from here
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h:136:4: error: use of deleted function 'std::thread::thread(std::thread&)'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from primes.cpp:4:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/thread:109:5: note: declared here
thread(thread&) = delete;
^~~~~~
我一般都不熟悉C ++和C。为什么会发生此错误?我该如何解决?
解决方法
这似乎是因为不允许复制线程。
您可以使用[d,e,f]
代替threads.push_back(std::move(th));
。
另一种选择是使用
threads.push_back(th);
代替
threads.push_back(thread(getPrimes,inp));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。