Search code examples
c++compiler-errorsg++ubuntu-18.04nullptr

Compiler errors with incorrect use of nullptr


I am trying the solution provided in this SO Q/ACompiler error while using shared_ptr with a pointer to a pointer and I am not able to use the solution provided in a proper way. I still get compilation errors on Ubuntu 18.04 with g++ version 7.3

Here is my minimum complete verifiable example to reproduce the problem

test.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;
};

test.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;
 }

This is the error I get

    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);
                                ^

What else have you tried ?

I tried initializing the nullptr in the header file as well

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

But that did not work either. Where am I going wrong ?

My Goal

All I am trying to do is the following - I am trying to initialize flane_pointers which is a vector of pointers to a nullptr. The declaration has been made in a header file as to what type it is and I am trying to initialize it in a .cpp file. While doing that I get the above compilation errors.

   flane_pointers(nullptr)

UPDATE

Could any of the answers explain whether the initialization provided in this Compiler error while using shared_ptr with a pointer to a pointer correct or not ?

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

To me (and I am a newbie to C++) that initialization looks like a function call as well. Is that incorrect ?


Solution

  • If your intent is to initialize flane_pointers to nullptr, you should use initialization of the below form:

    shared_ptr<shared_ptr<ProxyNode>> flane_pointers = nullptr;
    

    in struct _test_

    or

    test1.flane_pointers = nullptr; 
    

    in main.

    The other form of initialization you are trying to do is interpreted as a function call in main and as a function declaration in struct _test_.

    In the linked post,

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

    is in main and can only be interpreted as a variable declaration and not a function call because it does not have a function call syntax as the variable is preceded by the type std::shared_ptr >.

    To avoid confusion, it is better (from C++11 onwards) to declare and initialize variables with the brace-enclosed initializer {}.