gpt4 book ai didi

c++ - vector.push_back()在推送线程时(在编译过程中)产生已删除的函数错误

转载 作者:行者123 更新时间:2023-12-01 15:10:12 25 4
gpt4 key购买 nike

我不明白为什么,但是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的新手。为什么会发生此错误?我该如何解决?

最佳答案

似乎是因为不允许复制线程。
您可以使用threads.push_back(std::move(th));代替threads.push_back(th);
另一种选择是使用

threads.push_back(thread(getPrimes, inp));
代替
thread th(getPrimes, inp);
threads.push_back(th);

关于c++ - vector.push_back()在推送线程时(在编译过程中)产生已删除的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63569587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com