1

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 ?

gansub
  • 1,164
  • 3
  • 20
  • 47
  • 3
    What are you expecting `test1.flane_pointers(nullptr);` to do? It looks like you're trying to call `flane_pointers` as if it was a function. – David Schwartz Apr 15 '19 at 03:51
  • @DavidSchwartz As a newbie to C++ I do not know. I was just trying out the solution given in the linked Q/A. – gansub Apr 15 '19 at 03:53
  • @gansub the qusetion you linked to did not suggest `test1.flane_pointers(nullptr);` – M.M Apr 15 '19 at 04:23
  • @gansub I downvoted because the code has no comments and when I asked what you were expecting the line of code you asked about to do, you didn't have an answer. A question asking how to do something that doesn't clearly explain what it is you're trying to do is just not a very good question and, IMO, deserves a downvote. – David Schwartz Apr 15 '19 at 14:18
  • @DavidSchwartz the post has since been updated - you might wanna take a second look (it appears to have added the info you requested earlier) – Zoe Apr 15 '19 at 17:46

3 Answers3

2

On this line:

test1.flane_pointers(nullptr);

You're trying to call flane_pointers as though it were a member function. shared_ptr can't be called like a function, so you get the compiler error.

If you want to initialize flane_pointers, you can just assign to it:

test1.flane_pointers = nullptr; 

Or alternatively, you could do the assignment when you create test1:

// Initialize test1 with a nullptr
_test_ test1{nullptr}; 
P.W
  • 26,289
  • 6
  • 39
  • 76
Alecto Irene Perez
  • 10,321
  • 23
  • 46
1

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 {}.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • thanks for your answer. So can you explain how it works in the linked Q/A ? std::shared_ptr > ptr2ptr2ProxyNode(nullptr) – gansub Apr 15 '19 at 03:54
  • If you can tell me which specific line are you referring to in the linked post, I can try to explain it. – P.W Apr 15 '19 at 03:57
1

The line

test1.flane_pointers(nullptr);

is treated a function call. That's the source of the error. Use assignment instead.

test1.flane_pointers = nullptr;

And

shared_ptr<shared_ptr<ProxyNode>> flane_pointers(nullptr);

is not a valid form of in-member initialization. You may use

shared_ptr<shared_ptr<ProxyNode>> flane_pointers{nullptr};

or

shared_ptr<shared_ptr<ProxyNode>> flane_pointers = nullptr;
R Sahu
  • 204,454
  • 14
  • 159
  • 270