2012-05-09 74 views
1

是否有人可以告訴我用在C使用函數getline()++這頭。我已經使用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 
} 
+0

你說的「不工作」是什麼意思?請更具體。 – suszterpatt

+0

請向您的問題添加代碼,該代碼使用getline。 –

+0

編輯包括後 – user1232138

回答

2

你需要iostream,並可能string如果這就是你正在使用的版本。既然你沒有提供額外的信息,爲什麼這樣做不起作用將不得而解。

+0

代碼,你可以找到什麼是錯。 – user1232138

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/

+0

包括代碼,你能找到最新的錯誤。 – user1232138

1

顯示你的函數ifstream而非istream,所以你必須包括<fstream>拿到的定義。

然而,你可能採取istream,除非出於某種原因,他們只能用一個文件工作過使功能更通用的更好。在這種情況下,你只需要<iostream><string>,你已經有了。