2017-03-01 59 views
-1

對不起,我創建了自己的類string與重載操作符和功能at()。我創造它是爲了在另一個班級中引入它。除了一個之外,我沒有任何問題。使用getline自己的類C++

我的錯誤是:

std::basic_istream<_Elem,_Traits>&std::getline(std::basic_istream<_Elem,_Traits>&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'String' 

的問題是在getline()功能。此函數不需要MY string作爲參數str。我是否可以找到另一個近似變體getline()以使用MY string來讀取我的文件?

爲什麼不能getline()與我的字符串一起工作,爲什麼它會等待一些真正的字符串?

我的功能是:

void City::readRecordings(char *fileName, char *num, std::vector<string> lines) 
{ 

    string str; 
    //std::ifstream fin(fileName); 
    readRecord >>str; 
    int readCount = 0; 
    int n = atoi(num); 
    readRecord.open(fileName); 
    while (!readRecord.eof()) 
    { 
     std::getline(readRecord, str);/*here is Error, It can't use my String str , it waits for usual string*/ 
     readCount++; 
     lines.push_back(str); 
    } 

    if (0 == readCount) 
    { 
     std::cout << "ERROR: The file, which you are trying to open, is empty or it stops out" << end; 
     exit(-1); 
    } 
    if (n > readCount) 
    { 
     std::cout << "!Warning!" << end; 
     std::cout << "You want to read " << n << " recordings" << " But AVAILABLE: " << readCount << " recordings" << end; 
     printRecordsFromFile(readCount, lines); 
    } 
    else 
    { 
     printRecordsFromFile(n, lines); 
    } 
} 
+0

天空是藍色的:因爲'std :: getline()'的第二個參數是'std :: string'(好,'std :: basic_string',是迂迴的)。這就是'std :: getline()'的意思。 –

+0

那麼錯誤信息中提到的_from'String'_是什麼。你自己的東西? –

+0

@πάνταῥεῖ是的,我做了一些實現我的字符串 –

回答

0

歡迎您來寫自己的getline功能,使用您的string類:

std::istream& 
std::getline(std::istream& input_stream, My_String& s, char delimiter = '\n') 
{ 
    //... 
    return input_stream; 
} 

有許多優點,使用std::string,而不是開發自己的字符串類。