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?