You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译报错:no matching function for call to 'construct_at' 问题分析

问题描述

我在头文件中定义了Session类,该类单独编译正常(User类仅包含用户名/密码及两个setter方法,此处省略其定义)。但在main函数中调用Session类的signUp方法时,出现报错,核心错误为no matching function for call to 'construct_at'。

Session类代码:

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函数代码:

#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.
错误原因分析
  • 核心问题:make_shared<User>(*newUser) 试图通过拷贝构造函数创建User对象,但User类没有可用的拷贝构造函数(编译器提示call to deleted constructor of 'User')。可能是你显式删除了User的拷贝构造,或者User类包含不可拷贝的成员,导致编译器自动删除了默认拷贝构造。
  • 单独编译Session类正常的原因:C++中,类的成员函数只有在被实际调用时才会触发模板实例化和代码生成。单独编译Session类时,signUp方法内的make_shared代码并未被实例化,编译器不会检查拷贝构造是否存在;只有在main函数调用signUp时,该代码才会被实例化,此时才会发现User缺少可用的拷贝构造函数,进而报错。
解决方法

有两种可行的修复方式:

方式一:为User类添加拷贝构造函数

在User类中显式定义拷贝构造函数,示例:

class User
{
private:
    string username;
    string password;
public:
    // 保留原有默认构造和setter
    User() = default;
    void setUsername(const string& name) { username = name; }
    void setPassword(const string& pwd) { password = pwd; }
    
    // 添加拷贝构造函数
    User(const User& other) : username(other.username), password(other.password) {}
};

方式二:直接用make_shared创建User并初始化,避免拷贝

这种方式更高效,还能避免手动new的内存泄漏风险:
修改Session类的signUp方法:

void signUp()
{
    while (true)
    {
        cout << "Please enter your username and password: \n";
        string username, password;
        cin >> username >> password;

        currentUser = make_shared<User>();
        currentUser->setUsername(username);
        currentUser->setPassword(password);
        break;
    }
}

如果User类支持带参数的构造函数,还可以进一步简化:

// 先给User添加带参数的构造函数
class User
{
public:
    User(const string& name, const string& pwd) : username(name), password(pwd) {}
    // ...其他成员
};

// 然后修改signUp:
void signUp()
{
    while (true)
    {
        cout << "Please enter your username and password: \n";
        string username, password;
        cin >> username >> password;

        currentUser = make_shared<User>(username, password);
        break;
    }
}

内容的提问来源于stack exchange,提问作者antoine1111

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 05:01:00