调用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; }
我很困惑,因为我在while循环的声明中使用了&符号,但编译器仍然说我使用了不正确的参数 编辑:结果是我无意中创建了一个函数。调用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; },c++,ifstream,getline,istream,C++,Ifstream,Getline,Istream,我很困惑,因为我在while循环的声明中使用了&符号,但编译器仍然说我使用了不正确的参数 编辑:结果是我无意中创建了一个函数。输入的真正声明是: ifstream input(string(filename)); 因为我必须从char*解析filename。我没有将其包含在原始代码中,因为我试图使其通用化,以便它适用于许多人。我对C++比较陌生。只需在input声明之外创建字符串,即可解决此问题。所以我做了: string fname(filename); ifstream input(fna
输入的真正声明是:
ifstream input(string(filename));
因为我必须从char*
解析filename
。我没有将其包含在原始代码中,因为我试图使其通用化,以便它适用于许多人。我对C++比较陌生。只需在input
声明之外创建字符串,即可解决此问题。所以我做了:
string fname(filename);
ifstream input(fname);
很抱歉。GetLine的参数是引用。在C++中,不需要显式地给函数提供引用。编译器将为您执行此操作
因此,您的函数调用:
std::getline(&input,&line)
将成为:
std::getline(input, line)
GetLine的参数是引用。在C++中,不需要显式地给函数提供引用。编译器将为您执行此操作
因此,您的函数调用:
std::getline(&input,&line)
将成为:
std::getline(input, line)
尝试确保您的std::getline(…)
使用了正确类型的变量作为参数
std::basic\u istream&input
std::basic_字符串和str
CharT delim
std::basic\u istream&input
std::basic_字符串和str
CharT delim
std::basic\u istream&input
std::basic_字符串和str
std::basic\u istream&input
std::basic_字符串和str
CharT delim
您应该能够将参数从其引用地址(&
)更改为其实际值(无&
)。当函数要求一个值时,通常可以直接将值传递给它
#包括
#包括
int main()
{
std::ifstream输入(“C:\\temp\\file.txt”);
std::字符串结果;
std::字符串行;
while(std::getline(输入,行)){
结果+=行;
}
printf(“成功!\n”);
返回0;
}
尝试确保您的std::getline(…)
使用了正确类型的变量作为参数
std::basic\u istream&input
std::basic_字符串和str
CharT delim
std::basic\u istream&input
std::basic_字符串和str
CharT delim
std::basic\u istream&input
std::basic_字符串和str
std::basic\u istream&input
std::basic_字符串和str
CharT delim
您应该能够将参数从其引用地址(&
)更改为其实际值(无&
)。当函数要求一个值时,通常可以直接将值传递给它
#包括
#包括
int main()
{
std::ifstream输入(“C:\\temp\\file.txt”);
std::字符串结果;
std::字符串行;
while(std::getline(输入,行)){
结果+=行;
}
printf(“成功!\n”);
返回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;
}
/\include“string\u helper.hpp”//包含函数的定义
#包括
#包含//在同一文件中的另一个函数中使用
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入(“file.txt”);
字符串结果;
字符行[100];
while(input.getline(第“\n”行)
{
结果+=行;
}
不能把这个代码放进去。它会暂时起作用。你可以把它变成通用的
//#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;
}
/\include“string\u helper.hpp”//包含函数的定义
#包括
#包含//在同一文件中的另一个函数中使用
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入(“file.txt”);
字符串结果;
字符行[100];
while(input.getline(第“\n”行)
{
结果+=行;
}
cout您正在传递变量的地址。对于初学者来说,&
表示变量类型上下文中的引用,但表示使用变量上下文中的地址,这有点令人困惑
对于工作调用,需要接受指针参数,而指针参数不接受,因此在给定参数的情况下,没有匹配的函数调用
传递引用不需要做任何特殊的操作。引用和传递给函数的值之间的区别是由函数而不是调用方来确定的
所以只要去掉那些符号,它就会正常工作
#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';
}
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入(“file.txt”);
字符串结果;
弦线;
while(getline(输入,行))
{
结果+=行;
}
cout您正在传递变量的地址。对于初学者来说,&
表示变量类型上下文中的引用,但表示使用变量上下文中的地址,这有点令人困惑
对于工作调用,需要接受指针参数,而指针参数不接受,因此在给定参数的情况下,没有匹配的函数调用
传递引用不需要做任何特殊的操作。引用和传递给函数的值之间的区别是由函数而不是调用方来确定的
所以只要去掉那些符号和