【问题标题】:Invalid use of incomplete type (for a pointer to atomic integer variable)不完整类型的无效使用(用于指向原子整数变量的指针)
【发布时间】:2019-12-29 14:50:04
【问题描述】:

当我写这篇文章时,

std::atomic<int> * tmp = new std::atomic<int>();

g++ 编译器返回错误提示

invalid use of incomplete type "struct std::atomic<int>"

为什么会出现这个错误?我该如何逃避?我是否需要将原子变量包装在一个类中并使用它的指针?

智能指针也会发生同样的事情。

std::shared_ptr<std::atomic<int>> tmp = std::make_shared<std::atomic<int>> ();

【问题讨论】:

  • 你需要#include &lt;atomic&gt;
  • ... 和#include&lt;memory&gt;std::shared_ptr
  • 我已经包含了内存,但是没有原子。现在试试。
  • 错误现在消失了。

标签: c++ pointers incomplete-type stdatomic


【解决方案1】:

不完整类型是您的编译器在这里给出的重要线索:编译器诊断现在非常好 - 非常值得一读!

这意味着您没有 #included 正确的文件 - 因为在编译时类型不完整

总是明确包含 C++ 标准库文件。在这种情况下,您需要 &lt;atomic&gt;&lt;memory&gt;

【讨论】:

    【解决方案2】:

    你需要

    #include <atomic>
    #include <memory>
    

    要访问您正在使用的原子和 shared_ptr。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多