QtCreator,cmake。防止遍历对象向量并将它们传递给函数。
Ошибка /home/bai/pplab1/main.cpp:19: ошибка: call to implicitly-deleted copy constructor of 'Detail'
for (Detail counter : listDetail) {
^ ~
编码:
#include <iostream>
#include <vector>
#include "machine.h"
#include "detail.h"
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
void Process(Detail &list_p) {
while(list_p.Get_machine_list().size() > 0 && list_p.Get_state() < 3) {
if (list_p.Get_last_time() > 0) {
int temp = list_p.Get_last_time();
list_p.Set_last_time(--temp);
}
std::this_thread::sleep_for(2s);
}
}
void Start(const std::vector<Detail> &listDetail) {
for (Detail counter : listDetail) {
Process(counter);
}
}
int main() {
std::vector<Machine>listMachine = {{1, "Collector"}, {2,"Painter"}, {3, "Polisher"}};
std::vector<Detail>listDetail = {{1, "LadaPriora", 3, listMachine[0], listMachine[1]}, {2, "KiaRio", 4, listMachine[2]},
{3, "FordFocus", 7, listMachine[1], listMachine[2]}, {4, "ToyotaPrado", 9, listMachine[0]}};
std::cout << "N | ";
for (auto &x : listMachine){
std::cout << " R" << x.Get_id() << " ";
}
std::cout << " | ";
for (auto &x : listDetail) {
std::cout << " P" << x.Get_id() << " ";
}
std::cout << std::endl;
Start(listDetail);
return 0;
}
UPD:更正了以下行:
void Start(std::vector<Detail> &listDetail) {
for (Detail &counter : listDetail) {
Process(counter);
}
}
但是出现了错误
/usr/include/c++/6.3.0/bits/stl_construct.h:75: ошибка: call to implicitly-deleted copy constructor of 'Detail'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
好吧,编译器直接告诉你:你的类没有复制构造函数。在你的for中,简单的参考一下。
此外,向量构造函数不能接受没有复制构造函数的对象(至少在这种形式中)。如果您使用的是标准 11,则只需创建一个空向量并用 emplace_back 填充它。