[英]No Matching Function for call GetLine()
我試圖在C ++中創建一個函數,該函數從文件中讀取所有行,然后將所有行連接為單個字符串,然后返回該字符串。 這是我用來完成此操作的代碼:
// at the top of the file
#include "string_helper.hpp" // contains definition for function
#include <vector>
#include <string.h> // used in another function within the same file
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
// the code I believe to be problematic
ifstream input("file.txt");
string result(0);
string line;
while (std::getline(&input,&line)) {
result += line;
}
但是,當我嘗試編譯此代碼時,出現以下錯誤:
<project root>/src/string_helper.cpp:52:35: error: no matching function for call to ‘getline(std::ifstream (*)(std::__cxx11::string), std::__cxx11::string*)’
while (std::getline(&input,&line)) {
^
In file included from /usr/include/c++/6.3.1/string:53:0,
from include/string_helper.hpp:22,
from <project root>/src/string_helper.cpp:19:
我查看了www.cplusplus.com,它列出了getline的定義為:
istream& getline (istream& is, string& str);
我很困惑,因為我在while循環的聲明中使用了&符號,但是編譯器仍然說我使用了不正確的參數。
編輯:原來我不小心創建了一個函數。 input
的實際聲明是:
ifstream input(string(filename));
因為我必須從char *
解析filename
。 我沒有將其包含在原始代碼中,因為我試圖使其通用,因此它適用於許多人。 我對C ++比較陌生。 只需在input
聲明之外創建字符串input
解決此問題。 所以我做了:
string fname(filename);
ifstream input(fname);
對不起。
GetLine的參數是引用。 在C ++中,您不需要顯式地為函數提供引用。 編譯器將為您完成此操作。
因此,您的函數調用:
std::getline(&input,&line)
會變成:
std::getline(input, line)
您正在傳遞變量的地址 。 有點混亂對於一個初學者&
指在可變型的上下文中參考而是指在使用可變的上下文地址。
對於該調用getline的工作,將需要接受指針參數,但不會接受,因此,根據提供的參數,沒有匹配的函數調用。
您不必做任何特殊的事情就可以傳遞參考。 引用和傳遞給函數的值之間的區別是由函數而不是調用者進行的。
因此,只需刪除那些“&”號,它就可以正常工作。
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream input("file.txt");
string result;
string line;
while (getline(input, line))
{
result += line;
}
cout << result << '\n';
}
您也不需要初始化result
字符串。 默認情況下,它被初始化為空字符串。
嘗試確保您的std::getline(...)
使用正確類型的變量作為其參數。
std::basic_istream<CharT,Traits>& input
std::basic_string<CharT,Traits,Allocator>& str
CharT delim
std::basic_istream<CharT,Traits>&& input
std::basic_string<CharT,Traits,Allocator>& str
CharT delim
std::basic_istream<CharT,Traits>& input
std::basic_string<CharT,Traits,Allocator>& str
std::basic_istream<CharT,Traits>& input
std::basic_string<CharT,Traits,Allocator>& str
CharT delim
您應該能夠將參數從其引用地址( &
)更改為實際值(no &
)。 當函數要求&value
,通常可以直接將值傳遞給它。
http://en.cppreference.com/w/cpp/string/basic_string/getline
#include <string>
#include <fstream>
int main()
{
std::ifstream input("C:\\temp\\file.txt");
std::string result;
std::string line;
while (std::getline(input, line)) {
result += line;
}
printf("Made it!\n");
return 0;
}
輸入此代碼。 您可以暫時使它通用,它將暫時起作用。
//#include "string_helper.hpp" // contains definition for function
#include <vector>
#include <string.h> // used in another function within the same file
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
ifstream input("file.txt");
string result;
char line[100];
while (input.getline(line,'\n'))
{
result += line;
}
cout<<"string = "<<result<<endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.