0

i use qcc (qnx 660) compiler (gcc 4.7.3).

I wanted to use a reference to an object in another object. For this I wanted to pass this in a constructor call and copy/assign it to a member instance.

As I got undefined reference errors again and again I got annoyed and converted the whole thing in a simpler way and changed a refrenz to an int variable, see example.

I came across this question, and wanted to solve it in the answer as given by @sellibitze.

Here is my code example:

foo.hpp:

#include <memory>
#include <utility>

class foo { public:

  explicit foo(std::shared_ptr<int> stuff);

private:

  std::shared_ptr<int> mstuff;

};

foo.cpp:

#include "foo.hpp"

explicit foo::foo(std::shared_ptr<int> stuff)
:mstuff(std::move(stuff))
{
  std::cout << "done" << std::endl;
}

main.cpp:

#include <cstdlib>
#include <iostream>

#include "foo.hpp"


int main(int argc, char *argv[]) 
{

  foo my_foo(std::make_shared<int>(10));

}

error message of compiler:

main.cpp:11: undefined reference to `foo::foo(std::shared_ptr)'

As this didn't work either, I got suspicious and recreated the example in the online compiler, https://godbolt.org/z/83hTMq1ef.

With the gcc 4.7.3 various messages come. With a gcc >9 you only get this message :

:17:1: error: 'explicit' outside class declaration 17 | explicit foo::foo(std::shared_ptr cnt) | ^~~~~~~~ Compiler returned: 1

What is the smartest way to pass a shared_ptr with gcc version 4.7.3?

  • 1
    Why `std::move(stuff)`? I thought that one of the points of a shared pointer was that it's actually *shared*. If you want unique single-ownership of the data perhaps you should use `std::unique_ptr` instead? – Some programmer dude Oct 18 '22 at 08:44
  • the error is not about the shared_ptr but about the `explicit`. Remove it from the definition – 463035818_is_not_an_ai Oct 18 '22 at 08:44
  • In your cpp, `explicit foo::foo(std::shared_ptr stuff)` , that `explicit` has no business being there. – WhozCraig Oct 18 '22 at 08:44
  • 1
    As for the "undefined reference" error, I ***guess*** it's because you don't build with all source or object files, so the linker just won't find that symbol. – Some programmer dude Oct 18 '22 at 08:45
  • Please, have a look at [explicit specifier](https://en.cppreference.com/w/cpp/language/explicit): _The explicit specifier may only appear within the decl-specifier-seq of the declaration of a constructor or conversion function (since C++11) **within its class definition**._ (Emphasis mine.) Hence, `explicit foo::foo(std::shared_ptr stuff)` in `foo.cpp` is just wrong (exactly how stated by your compiler complaint). – Scheff's Cat Oct 18 '22 at 08:45
  • @Someprogrammerdude true, but moving a shared pointer when applicable is an optimization because it can skip the atomic reference increment/decrement. – Quentin Oct 18 '22 at 08:52
  • Ok, i just removed the *explicit * specifier of my definition, but i still can build it under gcc >V. 9 .. So not for my system, qnx 6.6 with gcc 4.7.3 :/ – zombieanfuehrer Oct 18 '22 at 08:52
  • @ Some programmer dude, in my use case I give foo a ref. to a object which is my GUI interface.. So I communicate in more object wiht my GUI outside foo – zombieanfuehrer Oct 18 '22 at 08:54
  • A little nitpicking: In the example code you show there's no references at all. You pass a `std::shared_ptr` object *by value*. Pointers and references are not the same. – Some programmer dude Oct 18 '22 at 09:10

0 Answers0