我不明白这一点:在抛出"std::length_error"实例后终止调用
I dont understand this: terminate called after throwing an instance of 'std::length_error'
首先,我不是英国人,所以我会尽我所能解释清楚。我抛出了这条线,其中saludo表示问候,retardo表示延迟,numero表示数量,我还创建了一些提示,表示这种情况会发生很多次。所以我要做的是创建10个线程,这将在屏幕上显示从5到15次,从100到300的延迟,他们是一个数字("Soy"数字),但我有这个错误,我无法解决。它工作2-3个线程,然后停止。谢谢顺便说一句。
#include <iostream>
#include <thread>
#include <string>
#include <chrono>
#include <time.h>
using namespace std;
void saludo(string m, int retardo, int numero) {
string tabs(numero - 1, 't');
cout << tabs << m << numero << +"n";
this_thread::sleep_for(chrono::milliseconds(retardo));
}
int main() {
int nthread = 10;
srand(time(NULL));
thread P[nthread];
int i = 0;
while(i<nthread){
int retardo = rand() % 201 + 100;
int veces = rand() % 11 + 5;
for (int x = 0; x<veces; ++x){
int numero = rand() % 10;
P[i] = thread(&saludo, "Soy ", retardo, numero);
P[i].join();
}
}
cout << "Finn";
return 0;
}
此错误是因为您可能将负数传递给std::string构造函数。rand() % 10
可能是0。你在做string tabs(numero - 1, 't');
如果numero
是0就有问题了
相关文章:
- 从C++实例化QML
- "error: no matching function for call to"构造函数错误
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- C++错误:"error: int aaa::bbb is protected within this context"