【问题标题】:No matching function for call to 'get line' while exploding爆炸时调用“get line”没有匹配的功能
【发布时间】:2015-02-13 23:45:04
【问题描述】:

使用此代码时调用“getline”没有匹配的函数:

ifstream myfile;
string line;
string line2;

myfile.open("example.txt");
while (! myfile.eof() )
{
    getline (myfile, line);
    getline (line, line2, '|');
    cout<<line2;
}

在 example.txt 我有这样的信息:

1|Name1|21|170
2|Name2|34|168

等等……

我真的很想得到这条线,直到 |字符...

我尝试了几个爆炸函数,但它们只是字符串类型,但我需要:

第一个 int

第二个是字符

第三和第四是浮动的。

我想做的事情真的很复杂,我无法很好地解释它。我希望有人能理解我。

【问题讨论】:

  • 我猜getline (line, line2, '|'); 第一个参数带有std::string 不是std::getline 的实际版本。
  • 我有:使用命名空间标准;这不是问题:(
  • 嗯,我是说没有getline(string, string, char)(我只是喜欢输入std::,讨厌using namespace std;)。错误消息非常不言自明。
  • 嗯...我从一个爆炸函数中得到:std::vector<:string> explode(std::string const & s, char delim) { std::vector结果; std::istringstream iss(s); for (std::string token; std::getline(iss, token, delim); ) { result.push_back(std::move(token)); } 返回结果; }
  • 您的代码中没有显示explode,并且错误引用了getline,除非您拥有getline 的非std 版本,否则就是问题所在。而且问题并不复杂(假设您的意思是string 而不是char 用于第二部分)。这是简单的字符串解析。

标签: c++ ifstream getline


【解决方案1】:

getline 接收模板basic_istream 的实例作为第一个参数。 string 不符合该要求。

你可以使用stringstream:

#include <sstream>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    string line;
    string line2;
    ifstream myfile("test/test.txt");

    while (getline(myfile, line))
    {
        stringstream sline(line);
        while (getline(sline, line2, '|'))
            cout << line2 << endl;
    }

    return 0;
}

【讨论】:

  • 几乎就是我想要的!我正在考虑使用此 line2 将信息插入课堂。所以我需要将它们保存到不同的变量中。但我想我会用 if 和 else 做点什么:D
  • @Nicox 我不明白你。 insert info into classsomething with if and else 是什么意思。顺便说一句,如果这对您有所帮助,请不要忘记upvopte。
  • 我有:char const * n[100];浮动 c;浮动 w;我应该将它们插入: s[++sCount].InportStudent(n,c,w);现在我开始制作它,如果喜欢: cout
  • @Nicox 为什么不对n 使用字符串?
  • 因为我不能使用 return n;如果它的字符串:(
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2011-03-23
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
相关资源
最近更新 更多