不完整类型的无效使用(对于指向原子整型变量的指针)
Invalid use of incomplete type (for a pointer to atomic integer variable)
当我写这篇文章时,
std::atomic<int> * tmp = new std::atomic<int>();
g++ 编译器returns 一个错误说
invalid use of incomplete type "struct std::atomic<int>"
为什么会出现这个错误?我怎样才能避免这个?我是否需要将原子变量包装在 class 中并改用它的指针?
同样的事情也发生在智能指针上。
std::shared_ptr<std::atomic<int>> tmp = std::make_shared<std::atomic<int>> ();
你需要
#include <atomic>
#include <memory>
访问您正在使用的原子和 shared_ptr。
不完整类型 是您的编译器在这里提供的重要线索:如今编译器诊断非常好 - 值得一读!
这意味着您没有 #include
d 正确的文件 - 因为在编译时类型不完整。
始终 显式包含 C++ 标准库文件。在这种情况下,您需要 <atomic>
和 <memory>
.
当我写这篇文章时,
std::atomic<int> * tmp = new std::atomic<int>();
g++ 编译器returns 一个错误说
invalid use of incomplete type "struct std::atomic<int>"
为什么会出现这个错误?我怎样才能避免这个?我是否需要将原子变量包装在 class 中并改用它的指针?
同样的事情也发生在智能指针上。
std::shared_ptr<std::atomic<int>> tmp = std::make_shared<std::atomic<int>> ();
你需要
#include <atomic>
#include <memory>
访问您正在使用的原子和 shared_ptr。
不完整类型 是您的编译器在这里提供的重要线索:如今编译器诊断非常好 - 值得一读!
这意味着您没有 #include
d 正确的文件 - 因为在编译时类型不完整。
始终 显式包含 C++ 标准库文件。在这种情况下,您需要 <atomic>
和 <memory>
.