1

C++ で getline() を使用するために使用するヘッダーを教えてください。利用した

#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

それらのどれも機能していないようです。

ここに私がこれまでに書いたコード全体があります

#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std;

class TextQuery
{
    typedef map<string, set<string::size_type> > word_map;
    TextQuery(ifstream &file)
    {
        FillLineVector(file);
        BuildMap(file);
    }

    void query(string);
private:
    word_map map;
    vector<string> LineVector;
    void FillLineVector(const ifstream&);
    void BuildMap(const ifstream&);
};

void TextQuery::FillLineVector(const ifstream& file)
{
    string line;
    while(getline(file,line)); //error:getline identifier not found
}
4

3 に答える 3

2

あなたが必要iostreamとし、おそらくstringそれがあなたが使用しているバージョンである場合。あなたが追加情報を提供しなかったので、なぜそれがうまくいかないのかは謎のままでなければなりません。

于 2012-05-09T19:03:06.450 に答える
2

人々に助けてもらいたい場合は、コードを投稿してください。とにかく、getline は次のように使用する必要があります。

// getline with strings
#include <iostream>
#include <string>
using namespace std;

int main () {
  string str;
  cout << "Please enter full name: ";
  getline (cin,str);
  cout << "Thank you, " << str << ".\n";
}

http://www.cplusplus.com/reference/string/getline/に示されているように

于 2012-05-09T19:04:08.947 に答える
0

関数はifstreamではなく取るので、その定義を取得するためistreamに含める必要があります。<fstream>

istreamただし、何らかの理由でファイルでしか機能しない場合を除いて、を使用して関数をより汎用的にする方がよいでしょう。その場合は、すでに持っているとが<iostream>必要です。<string>

于 2012-05-09T20:28:50.403 に答える