我在头文件中有一个这个类,它自己编译得很好。我省略了类用户的定义,它只是带有两个setter的用户名/密码。
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;
};
但是,当我从main调用signUp时
#include <iostream>
#include <memory>
#include "blocker.h"
using namespace std;
int main()
{
Session *session = new Session();
session->signUp();
return 0;
}
我犯了一个很长的错误,我无法理解。
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.
有人能解释一下这里发生了什么吗?似乎我无法从用户创建一个指针,但我仍然不理解错误消息,也不明白为什么类会成功编译。
发布于 2022-09-14 09:52:41
以下是删除错误后重写的注册函数
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);
}
User
与new
的分配已被删除。shared_ptr
(和unique_ptr
)的目的是避免手动分配内存。
此外,冗余的同时循环已被删除。
您可以通过向User
添加用户名/密码构造函数来改进这段代码(假设它还没有出现)。
class User
{
public:
User(const std::string& user, const std::string& pass) { ... }
...
};
然后你可以让make_shared
使用这个新的构造函数
void signUp()
{
cout << "Please enter your username and password: \n";
string username, password;
cin >> username >> password;
currentUser = make_shared<User>(username, password);
}
https://stackoverflow.com/questions/73714773
复制相似问题