由于类之间的相互依赖性,试图引用已删除的函数

attempting to reference a deleted function because of inter dependency between classes

本文关键字:引用 删除 函数 依赖性 之间 于类      更新时间:2024-09-27

我收到attempting to reference a deleted function错误,我觉得这是因为类之间的相互依赖性。

Location.h

#ifndef A_LOCATION_H
#define A_LOCATION_H
struct location {
double lat;
double lon;
double alt;
};
#endif //A_LOCATION_H

P.h

#ifndef A_P_H
#define A_P_H
#include <vector>
#include <mutex>
#include <memory>
#include "Location.h"
class C;
class P {
std::vector<std::shared_ptr<C>> C_List;
struct location loc {};
public:
P() = default;
~P() = default;
std::mutex mut;
void add_child(const std::string& th_name);
void del();
void set_data(double lat, double lon, double alt);
struct location get_data();
};
#endif //A_P_H

P.cpp

#include <iostream>
#include "C.h"    
#include "P.h"
void P::add_child(const std::string& th_name) {
std::lock_guard<std::mutex> lg(mut);
auto& ref = C_List.emplace_back(std::make_shared<C>());
ref->set_name(th_name);
ref->set_P(this);
ref->start();
}
void P::del() {
std::lock_guard<std::mutex> lg(mut);
for (auto& c : C_List)
c->terminate = true;
for (auto& c : C_List)
c->wait();
C_List.clear();
}
struct location P::get_data() {
std::lock_guard<std::mutex> lg(mut);
return loc;
}
void P::set_data(double lat, double lon, double alt) {
std::lock_guard<std::mutex> lg(mut);
loc.lat = lat;
loc.lon = lon;
loc.alt = alt;
}

C.h

#ifndef A_C_H
#define A_C_H
#include <string>
#include <thread>
#include <chrono>
#include <atomic>
class P;
class C {
P *p {};
std::string name {};
std::thread th {};
struct location loc {};
void run();
public:
C() = default;
~C() = default;
void set_P(P* p);
void set_name(const std::string& name);
void start();
void wait();
std::atomic<bool> terminate {false};
};
#endif //A_C_H

C.cpp

#include <iostream>
#include "P.h"
#include "C.h"
void C::run() {
while (!terminate) {
std::cout << name << std::endl;
{
auto loc = p->get_data();
// perform calculation based on P's object location, and it's current location
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
}
}
void C::set_P(P* p) {
this->p = p;
}
void C::set_name(const std::string& name) {
this->name = name;
}
void C::start() {
th = std::thread(&C::run, this);
}
void C::wait() {
th.join();
}

Main.cpp

#include <iostream>
#include "P.h"
int main() {
P p = P();
p.add_child("C1");
p.add_child("C2");
p.add_child("C3");
char input;
std::cin >> input;
p.del();
}

此外,当调用P's对象的del函数时,还会出现一种死锁。我不知道如何解决这个问题?

这是我得到的错误的简短描述

C:Program Files (x86)Microsoft Visual Studio2019BuildToolsVCToolsMSVC14.26.28801includexmemory(671): error C2280: 'C::C(const C &)': attempting to reference a deleted function
C:UsersHARSHADesktopLC2022AC.h(33): note: compiler has generated 'C::C' here
C:UsersHARSHADesktopLC2022AC.h(33): note: 'C::C(const C &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::thread::thread(const std::thread &)'
C:Program Files (x86)Microsoft Visual Studio2019BuildToolsVCToolsMSVC14.26.28801includethread(93): note: 'std::thread::thread(const std::thread &)': function was explicitly deleted

std::thread、std::atomic、std::mutex类型不可复制,因此编译器无法生成默认的复制构造函数和复制附件。

您必须为C和p类编写自己的复制构造函数。

P(P const& arg){ ... do stuff ... }