Hello,
I'm trying to learn unique_ptr, so I'm playing with it to get some idea.
I tried to write a function that returns a unique_ptr; called the function in main(). But my code produces an error, which I'm not able to understand. Kindly help me in resolving this.
#include <iostream>
#include <memory>
using namespace std;
class String
{
string d_string;
public:
String() { cout << "Default ctor" << endl; }
String(string const & obj) : d_string(obj) { cout << "Parameterized Ctor" << endl; }
String(String && tmp) : d_string(tmp.d_string)
{
tmp.d_string = nullptr;
cout << "Move Ctor" << endl;
}
String(String const & obj) : d_string(obj.d_string)
{
cout << "Copy Ctor" << endl;
}
~String() { cout << "Dtor" << endl; }
friend ostream & operator<<(ostream & out, unique_ptr<String> & obj);
String & operator=(String const & rhs)
{
d_string = rhs.d_string;
return *this;
}
void setVal(string const & str)
{
d_string = str;
}
string getVal() const { return d_string; }
};
// I haven't used this as it was reporting error.
// So added a getVal() member function
ostream & operator<<(ostream & out, unique_ptr<String> & obj)
{
out << obj->d_string << endl;
return out;
}
unique_ptr<string> && f()
{
unique_ptr<String> p;
p->setVal(string("rahul"));
return move(p);
}
int main()
{
unique_ptr<String> ptr = f();
cout << ptr->getVal() << endl;
}
#include <iostream>
#include <memory>
using namespace std;
class String
{
string d_string;
public:
String() { cout << "Default ctor" << endl; }
String(string const & obj) : d_string(obj) { cout << "Parameterized Ctor" << endl; }
String(String && tmp) : d_string(tmp.d_string)
{
tmp.d_string = nullptr;
cout << "Move Ctor" << endl;
}
String(String const & obj) : d_string(obj.d_string)
{
cout << "Copy Ctor" << endl;
}
~String() { cout << "Dtor" << endl; }
friend ostream & operator<<(ostream & out, unique_ptr<String> & obj);
String & operator=(String const & rhs)
{
d_string = rhs.d_string;
return *this;
}
void setVal(string const & str)
{
d_string = str;
}
string getVal() const { return d_string; }
};
// I haven't used this as it was reporting error.
// So added a getVal() member function
ostream & operator<<(ostream & out, unique_ptr<String> & obj)
{
out << obj->d_string << endl;
return out;
}
unique_ptr<string> && f()
{
unique_ptr<String> p;
p->setVal(string("rahul"));
return move(p);
}
int main()
{
unique_ptr<String> ptr = f();
cout << ptr->getVal() << endl;
}
To copy to clipboard, switch view to plain text mode
And the error is
main.cpp: In function ‘std::unique_ptr<std::__cxx11::basic_string<char> >&& f()’:
main.cpp:47:16: error: invalid initialization of reference of type ‘std::unique_ptr<std::__cxx11::basic_string<char> >&&’ from expression of type ‘std::remove_reference<std::unique_ptr<String>&>::type {aka std::unique_ptr<String>}’
return move(p);
~~~~^~~
main.cpp: In function ‘int main()’:
main.cpp:52:31: error: conversion from ‘std::unique_ptr<std::__cxx11::basic_string<char> >’ to non-scalar type ‘std::unique_ptr<String>’ requested
unique_ptr<String> ptr = f();
~^~
make: *** [Makefile:6: main.o] Error 1
main.cpp: In function ‘std::unique_ptr<std::__cxx11::basic_string<char> >&& f()’:
main.cpp:47:16: error: invalid initialization of reference of type ‘std::unique_ptr<std::__cxx11::basic_string<char> >&&’ from expression of type ‘std::remove_reference<std::unique_ptr<String>&>::type {aka std::unique_ptr<String>}’
return move(p);
~~~~^~~
main.cpp: In function ‘int main()’:
main.cpp:52:31: error: conversion from ‘std::unique_ptr<std::__cxx11::basic_string<char> >’ to non-scalar type ‘std::unique_ptr<String>’ requested
unique_ptr<String> ptr = f();
~^~
make: *** [Makefile:6: main.o] Error 1
To copy to clipboard, switch view to plain text mode
I have no idea what's happening. Please guide me if my implementation is wrong. Thanks.
Bookmarks