wutheringcoo 2018-10-14 04:27 采纳率: 0%
浏览 1603

C++中遇到ifstream问题

问题描述如下:
error: no matching function for call to 'std::basic_ifstream::basic_ifstream(std::__cxx11::string&)'|
源码如下:(请问哪位大神解答下?)

#include <iostream>
#include <vector>
#include<fstream>
#include<string.h>
#include <cstring>
//#include<string>
using namespace std;
vector < vector <char> > read_map(string file_name);
vector <char> read_line(string s);

int main () {

    read_map("maps/m1.txt");
    // TODO: Nothing to do here

    return 0;
}

vector < vector <char> > read_map(string file_name) {
    ifstream infile(file_name);
    vector < vector <char> > map;
    if (infile.is_open()) {

        char color;
        vector <char> row;

        string line;

        while (std::getline(infile, line)) {
            row = read_line(line);
            map.push_back(row);
        }
    }
    return map;
}

vector <char> read_line(string s) {
    vector <char> row;

    size_t pos = 0;
    string token;
    string delimiter = " ";
    char cell;

    while ((pos = s.find(delimiter)) != std::string::npos) {
        token = s.substr(0, pos);
        s.erase(0, pos + delimiter.length());

        cell = token.at(0);
        row.push_back(cell);
    }

    return row;
}

图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2018-10-14 05:09
    关注

    explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
    它不接收string,而是需要const char *

    char *pfilename = file_name.c_str();
    ifstream infile((const char *)pfilename);

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog