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

VS2022 17.11.3 64位C++20代码构建报错:std::construct_at无匹配重载

解决C2672 'std::construct_at' 编译错误

你的代码错误并非编译器底层问题,而是类型不匹配导致的:

  • 你定义的data是vector<int>,仅能存储int类型元素
  • 但调用data.emplace_back(tempdata)时,传入的tempdata是vector<int>类型,两者类型不兼容,编译器无法找到匹配的std::construct_at重载函数,因此触发报错。

修正方案1:存储二维整数列表(每行作为一个子vector)

将data的类型改为vector<vector<int>>,即可存储每行的整数集合:

#include <cstdio>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>

using std::vector;

vector<vector<int>> read_input() {
    std::string line;
    std::ifstream fp;
    vector<vector<int>> data; // 修改为二维vector类型
    int word;

    fp.open("input.txt");
    if (fp) {
        while (getline(fp, line, '\n')) {
            vector<int> tempdata;
            std::istringstream ss(line);
            while (ss >> word) {
                tempdata.push_back(word);
            }
            data.emplace_back(tempdata); // 类型匹配,可正常构造
        }
        
        fp.close();
    }
    else
        perror("File opening failed");
    
    return data;
}

修正方案2:将所有整数合并到一维vector中

如果需求是把文件内所有整数存入同一个一维vector,直接将读取到的元素逐个添加到data即可:

#include <cstdio>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>

using std::vector;

vector<int> read_input() {
    std::string line;
    std::ifstream fp;
    vector<int> data;
    int word;

    fp.open("input.txt");
    if (fp) {
        while (getline(fp, line, '\n')) {
            std::istringstream ss(line);
            while (ss >> word) {
                data.push_back(word); // 直接将整数添加到data
            }
        }
        
        fp.close();
    }
    else
        perror("File opening failed");
    
    return data;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 12:46:06