c++ - 错误使用 nullptr 的编译器错误

标签 c++ compiler-errors g++ ubuntu-18.04 nullptr

我正在尝试此 SO Q/A 中提供的解决方案 Compiler error while using shared_ptr with a pointer to a pointer而且我无法以正确的方式使用提供的解决方案。我在使用 g++ 7.3 版的 Ubuntu 18.04 上仍然遇到编译错误

这是我重现问题的最小完整可验证示例

测试.h

# include <memory> 
using std::shared_ptr;
using std::unique_ptr;
struct DataNode
{
 shared_ptr<DataNode> next;
} ;


struct ProxyNode
{
 shared_ptr<DataNode> pointers[5];
} ;


struct _test_
{
  shared_ptr<shared_ptr<ProxyNode>> flane_pointers;
};

测试.cpp

 #include <stdint.h>
 #include "test.h"


 shared_ptr<DataNode> newNode(uint64_t key);
 shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node);
 struct _test_ test1;
 int main(void)
 {

   test1.flane_pointers(nullptr);
   shared_ptr<DataNode> node = newNode(1000);
 }

 shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node) {

 shared_ptr<ProxyNode> proxy(new ProxyNode());
 return proxy;
 }


 shared_ptr<DataNode> newNode(uint64_t key) {

 shared_ptr<DataNode> node(new DataNode());
 return node;
 }

这是我得到的错误

    test.cpp: In function ‘int main()’:
    test.cpp:11:31: error: no match for call to   ‘(std::shared_ptr<std::shared_ptr<ProxyNode> >) (std::nullptr_t)’
    test1.flane_pointers(nullptr);
                                ^

您还尝试过什么?

我也尝试在头文件中初始化nullptr

  struct _test_
  {
   shared_ptr<shared_ptr<ProxyNode>> flane_pointers(nullptr);
  };

但这也不起作用。我哪里出错了?

我的目标

我正在尝试做的是以下 - 我正在尝试初始化 flane_pointers,它是一个指向 nullptr 的指针 vector 。关于它是什么类型的声明已在头文件中进行,我正试图在 .cpp 文件中对其进行初始化。这样做时,我遇到了上述编译错误。

   flane_pointers(nullptr)

更新

任何答案都可以解释此 Compiler error while using shared_ptr with a pointer to a pointer 中提供的初始化是否对不对?

  std::shared_ptr<std::shared_ptr<ProxyNode> > ptr2ptr2ProxyNode(nullptr);

对我(我是 C++ 的新手)来说,初始化看起来也像一个函数调用。这是不正确的吗?

最佳答案

在这一行:

test1.flane_pointers(nullptr);

您正在尝试调用 flane_pointers,就好像它是一个成员函数一样。 shared_ptr 不能像函数一样被调用,所以你会得到编译器错误。

如果你想初始化flane_pointers,你可以直接赋值给它:

test1.flane_pointers = nullptr; 

或者,您可以在创建 test1 时进行分配:

// Initialize test1 with a nullptr
_test_ test1{nullptr}; 

关于c++ - 错误使用 nullptr 的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55682351/

相关文章:

c++ - 编译时指定g++依赖的位置

c++ - 结构数组不会初始化

c# - 要在 C# 中使用的 PInvoke C++ 函数

gcc - 交叉 mingw 编译因未知伪操作而失败

c++ - 为什么别名模板给出冲突的声明?

c++ - 另一个系统上的应用程序在启动时崩溃,没有 sudo 的错误消息,非 sudo 的段错误

c++ - 如何解析函数及其参数

c++ - boost 编译问题

java - Android Studio Flamingo 出现“compileDebugJavaWithJavac”错误

iphone - 在iPhone而非iPhone模拟器上编译时出错