[英]no instance of overloaded function "getline" matches the argument list -- argument types are: (std::ifstream,char)
beginner c++ user here I have trying to take in lines from a file in put it into a string array. beginner c++ user here 我试图从文件中获取行并将其放入字符串数组中。 The file has each word on its own line.
该文件的每个单词都在其自己的行中。 Whenever I tested the array it appeared it was loading each letter into the array.
每当我测试数组时,它似乎正在将每个字母加载到数组中。 So it was chars instead each word as a string.
所以它是字符而不是每个单词作为一个字符串。 The first thing in each file is a number that will be the array size.
每个文件中的第一件事是一个数字,它将是数组的大小。 I was supposed to dynamically allocate the array's which I believe I did correctly.
我应该动态分配数组,我相信我做对了。 Any help is welcome.
欢迎任何帮助。 I am using VScode, and the issue that getline gives me is, no instance of overloaded function "getline" matches the argument list -- argument types are: (std::ifstream,char).
我正在使用 VScode,getline 给我的问题是,没有重载 function“getline”的实例与参数列表匹配——参数类型是:(std::ifstream,char)。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int showMenuGetChoice();
string createStringArray(int);
void getInfoFromUser(string, string, int);
void printStory(int, string, int);
// Calls all the other functions to make a working Madlibs game.
int main ()
{
int UserChoice;
ifstream QuestionFile;
int arraySize;
string Question;
int test = 0;
do {
cout <<"Let's Play some Madlibs!!" << endl << endl;
UserChoice=showMenuGetChoice();
if (UserChoice == 1)
{
ifstream QuestionFile;
QuestionFile.open("starWars.txt");
QuestionFile >> arraySize;
cin.ignore();
string QuestionArray=createStringArray(arraySize);
if(QuestionFile.is_open())
{
QuestionArray[arraySize];
for(int i=0; i < arraySize; i++)
{
getline(QuestionFile, QuestionArray[i]);
}
}
for(int i=0; i < arraySize; i++)
{
cout << QuestionArray[i] << endl;
}
}
// Takes in a integer and creates a array of that size. Dynamically allocates the array and returns a point to this array.
string createStringArray(int n)
{
string*QuestionArray = new string[n];
return *QuestionArray;
}
You're getting the error:你收到错误:
no instance of overloaded function "getline" matches the argument list -- argument types are: (std::ifstream,char).
没有重载的实例 function “getline”匹配参数列表——参数类型是:(std::ifstream,char)。
Because of a problem with your "getline" call:由于您的“getline”调用有问题:
getline(QuestionFile, QuestionArray[i]);
std::getline
accepts two parameters, a std::istream
reference and a std::string
reference. std::getline
接受两个参数,一个std::istream
引用和一个std::string
引用。 QuestionFile
is an istream
, so that's okay, however if you review the declaration of QuestionArray
: QuestionFile
是一个istream
,所以没关系,但是如果您查看QuestionArray
的声明:
string QuestionArray=createStringArray(arraySize);
You should notice that QuestionArray is a variable of string
type.您应该注意到 QuestionArray 是一个
string
类型的变量。 I don't think this is what you intended.我不认为这是你想要的。 You should always carefully choose the type of variables you declare in C++, otherwise you are working against the type safety the compiler is trying to provide you with.
您应该始终谨慎选择您在 C++ 中声明的变量类型,否则您将违背编译器试图为您提供的类型安全。
The i
th element of the string QuestionArray
is a char
, not a string
.字符串
QuestionArray
的第i
个元素是一个char
,而不是一个string
。 This is the cause of the compiler error.这是编译器错误的原因。 You are trying to place the line into a single character element instead of a string.
您试图将行放入单个字符元素而不是字符串中。
Hopefully with the above explanation, the compiler message provided will make more sense now.希望有了上面的解释,提供的编译器消息现在会更有意义。 It's providing the same information, just a little more condensed.
它提供相同的信息,只是更简洁一点。
With that knowledge, lets address the createStringArray
function:有了这些知识,让我们解决
createStringArray
function:
string createStringArray(int n)
{
string*QuestionArray = new string[n];
return *QuestionArray;
}
You declare the function to return a single string, when I expect you want the function to return an sequence of strings.当我希望 function 返回字符串序列时,您声明 function 返回单个字符串。 The correct way to return an sequence of strings is to use
std::vector<std::string>
, but if you must use risky, outdated, manual memory management you'll need to return a pointer to string, not a string from this function. With that knowledge you should be able to correct this function, the original QuestionArray
variable type, and the compiler error should go away.返回字符串序列的正确方法是使用
std::vector<std::string>
,但是如果你必须使用有风险的、过时的、手动的 memory 管理,你需要返回一个指向字符串的指针,而不是来自这个 function。有了这些知识,你应该能够更正这个 function,原始的QuestionArray
变量类型,并且编译器错误应该 go 消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.