gpt4 book ai didi

c++ - 试图为原子 C++ 引用已删除的函数

转载 作者:行者123 更新时间:2023-11-27 22:42:48 33 4
gpt4 key购买 nike

我有一段用于 Visual Studio 2013 的代码。现在我试图在 Visual Studio 2017 中构建相同的代码,它会提示。这是我正在尝试执行的代码。

#include <array>
#include <atomic>

int main()
{

using TrdRobotStateArray = std::array<std::atomic<double>, 6>;
TrdRobotStateArray mCurrentPose = { 0.3 };
printf("%0.3f", mCurrentPose[0]);
return 0;
}

有了这个,我得到了这个错误:

error C2280: 'std::atomic<double>::atomic(const std::atomic<double> &)': attempting to reference a deleted function

这段代码不是我写的,我正在尝试读入原子变量。但是我仍然不太确定错误是怎么回事。关于原子的解释将不胜感激。谢谢!

更新:

以下是此代码附带的所有错误和警告。所以它会在未来帮助其他人。

1>AtomicTest.cpp
1>AtomicTest.cpp(13): error C4839: non-standard use of class
'std::atomic<double>' as an argument to a variadic function
1>AtomicTest.cpp(13): note: the constructor and destructor will not be
called; a bitwise copy of the class will be passed as the argument
1>AtomicTest.cpp(11): note: see declaration of 'std::atomic<double>'
1>AtomicTest.cpp(13): error C2280: 'std::atomic<double>::atomic(const
std::atomic<double> &)': attempting to reference a deleted function
1>C:\Program Files (x86)\Microsoft Visual
Studio\2017\Professional\VC\Tools\MSVC\14.11.25503\include\atomic(689):
note: see declaration of 'std::atomic<double>::atomic'
1>C:\Program Files (x86)\Microsoft Visual
Studio\2017\Professional\VC\Tools\MSVC\14.11.25503\include\atomic(689):
note: 'std::atomic<double>::atomic(const std::atomic<double> &)': function
was explicitly deleted
1>AtomicTest.cpp(13): warning C4477: 'printf' : format string '%0.3f'
requires an argument of type 'double', but variadic argument 1 has type
'std::atomic<double>'
1>Done building project "AtomicTest.vcxproj" -- FAILED.

最佳答案

您必须显式加载值才能编译它:

printf("%0.3d", mCurrentPose[0].load());

否则,它将尝试为 printf() 复制原子变量本身,这显然不是本意。

关于c++ - 试图为原子 C++ 引用已删除的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47082652/

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