I have a this class in a header file which compiles fine by itself. I omit the definition of the class User which is just username/password with two setters.
class Session
{
public:
shared_ptr<User> getCurrentUser() { return currentUser; }
void signUp()
{
while (true)
{
cout << "Please enter your username and password: \n";
string username, password;
cin >> username >> password;
User *newUser = new User();
newUser->setUsername(username);
newUser->setPassword(password);
currentUser = make_shared<User>(*newUser);
break;
}
}
private:
shared_ptr<User> currentUser = nullptr;
};
However, when I call signUp from main
#include <iostream>
#include <memory>
#include "blocker.h"
using namespace std;
int main()
{
Session *session = new Session();
session->signUp();
return 0;
}
I get this really long error that I cannot understand.
In file included from main2.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:37:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/string:522:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/algorithm:653:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/functional:500:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__functional/function.h:18:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/allocator_traits.h:298:9: error: no matching function for call to 'construct_at'
_VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
^~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__config:858:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_ABI_NAMESPACE
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/shared_ptr.h:296:37: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<User>>::construct<User, User &, void, void>' requested here
allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/shared_ptr.h:1106:55: note: in instantiation of function template specialization 'std::__shared_ptr_emplace<User, std::allocator<User>>::__shared_ptr_emplace<User &>' requested here
::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/shared_ptr.h:1115:19: note: in instantiation of function template specialization 'std::allocate_shared<User, std::allocator<User>, User &, void>' requested here
return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
^
./blocker.h:39:27: note: in instantiation of function template specialization 'std::make_shared<User, User &, void>' requested here
currentUser = make_shared<User>(*newUser);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/construct_at.h:35:16: note: candidate template ignored: substitution failure [with _Tp = User, _Args = <User &>]: call to deleted constructor of 'User'
constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
^
1 error generated.
Could someone explain what's happening here? Seems like I cannot create a pointer from a a User, but I still don't understand the error message or why the class compiled succesfully.
Here's your signup function rewritten with the errors removed
void signUp()
{
cout << "Please enter your username and password: \n";
string username, password;
cin >> username >> password;
currentUser = make_shared<User>();
currentUser->setUsername(username);
currentUser->setPassword(password);
}
The allocation of User
with new
has been removed. The point of shared_ptr
(and unique_ptr
) is to avoid manually allocating memory.
Also the redundant while loop has been removed.
You could improve this code by adding a username/password constructor to User
(assuming it's not already there).
class User
{
public:
User(const std::string& user, const std::string& pass) { ... }
...
};
then you could get make_shared
to use that new constructor
void signUp()
{
cout << "Please enter your username and password: \n";
string username, password;
cin >> username >> password;
currentUser = make_shared<User>(username, password);
}