由于类之间的相互依赖性,试图引用已删除的函数
attempting to reference a deleted function because of inter dependency between classes
我收到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 ... }
相关文章:
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- C++ 尝试在不存在的构造函数中引用已删除的函数(使用 rapidJson)
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 在引用初始化中使用已删除的复制构造函数进行复制初始化
- CMake 引用我在 ~/bin 中创建的随机 shell 脚本(即使我删除了它)?
- 删除对象(具有不同类型)的引用时会发生什么情况?
- 使用函数引用指向节点的指针删除链表中的节点?
- unique_ptr实现接口时对已删除函数的引用
- 无法引用该函数" std:: variant <_Types...> operator =",因为此函数已被删除
- 从尝试引用已删除函数的矢量 C++ 中删除对象
- unique_ptr,您引用的是已删除的函数
- 试图引用已删除函数数组的相等运算符
- 错误:在类型 "blah blah" 的绑定引用中删除限定符以初始化"some other blah blah"
- 使用 future 时获取 C2280(尝试引用已删除的函数)
- 删除通过取消引用新对象初始化的对象
- C++引用已删除函数错误
- 是否可以删除取消引用的指针
- 尝试引用已删除的函数
- 如何在C++上使用对象引用删除对象
- BOOST共享指针导致冗余引用删除