首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >没有调用“construct_at”的匹配函数

没有调用“construct_at”的匹配函数
EN

Stack Overflow用户
提问于 2022-09-14 09:45:34
回答 1查看 386关注 0票数 0

我在头文件中有一个这个类,它自己编译得很好。我省略了类用户的定义,它只是带有两个setter的用户名/密码。

代码语言:javascript
运行
复制
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时

代码语言:javascript
运行
复制
#include <iostream>
#include <memory>
#include "blocker.h"

using namespace std;

int main()
{
    Session *session = new Session();
    session->signUp();
    return 0;
}

我犯了一个很长的错误,我无法理解。

代码语言:javascript
运行
复制
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.

有人能解释一下这里发生了什么吗?似乎我无法从用户创建一个指针,但我仍然不理解错误消息,也不明白为什么类会成功编译。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-14 09:52:41

以下是删除错误后重写的注册函数

代码语言:javascript
运行
复制
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);
}

Usernew的分配已被删除。shared_ptr (和unique_ptr)的目的是避免手动分配内存。

此外,冗余的同时循环已被删除。

您可以通过向User添加用户名/密码构造函数来改进这段代码(假设它还没有出现)。

代码语言:javascript
运行
复制
class User
{
public:
    User(const std::string& user, const std::string& pass) { ... }
    ...
};

然后你可以让make_shared使用这个新的构造函数

代码语言:javascript
运行
复制
void signUp()
{
     cout << "Please enter your username and password: \n";
     string username, password;
     cin >> username >> password;
     currentUser = make_shared<User>(username, password);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73714773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档