[英]error: cannot convert ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘char**’ for getline() function
I had this program working a while ago but I recently came back to look at it for another project I am working on and now I am getting errors while trying to use getline()
前段时间我有这个程序工作,但我最近回来查看我正在处理的另一个项目,现在我在尝试使用getline()
时遇到错误
The other file is just a text file that has the player's name, their position and other data about them as such:另一个文件只是一个文本文件,其中包含玩家的姓名、他们的 position 以及关于他们的其他数据:
Bill Quarter_Back 70 0 8754 0 573比尔 Quarter_Back 70 0 8754 0 573
before adding:: infront of the getline function I was getting the error在添加:: getline function 之前我收到了错误
error: no matching function for call to 'getline(std::ifstream&, int&, char)'错误:没有匹配的 function 调用 'getline(std::ifstream&, int&, char)'
getline(input, p[i].position, ' '); "
and I don't know why.我不知道为什么。
Here's the code:这是代码:
#include <iostream>
#include <fstream>
#include <string>
struct player{
int position, touchDowns, catches, passingYards, recievingYards, rushingYards;
std::string name;
};
using namespace std;
void printPlayer(player p[], ifstream &input);
void createArray(player p[], ifstream &input);
int main() {
ifstream input;
player p[10];
int choice;
while(choice != 99){
cout << "Select one of the following options: " << "1: To print a player's data\n2: To print the entire data\n3: To update a player's touch downs\n4: To update a player's number of catches\n5: To update a player's passing yards\n6: To update a player's receiving yards\n7: To update a player's rushing yards\99: To quit the program" <<endl;
cin >> choice;
switch (choice){
case 1:
printPlayer(p, input);
}
if(choice == 99){
break;
}
}
return 0;
}
void createArray(player p[], ifstream &input){
for(int i =0;i < 10; i++){
getline(input, p[i].name, ' ');
getline(input, p[i].position, ' ');
getline(input, p[i].touchDowns, ' ');
getline(input, p[i].catches, ' ');
getline(input, p[i].passingYards, ' ');
getline(input, p[i].recievingYards);
getline(input, p[i].rushingYards);
}
}
void printPlayer(player p[], ifstream &input){
int wantedPlayer;
string guyname;
cout << "Enter players name: ";
cin >> wantedPlayer;
cout << endl;
for(int i =0; i < 10; i++){
guyname = p[i].name;
cout << p[i].name << " " << p[i].position << " " << p[i].touchDowns << " " << p[i].catches << " " << p[i].passingYards << " " << p[i].recievingYards << " " << p[i].rushingYards << "\n" << endl;
}
}
Without the using namespace std;
没有using namespace std;
statement ( which you should not use ), your calls to getline()
would not be calling C++'s std::getline()
, but C's getline()
from <stdio.h>
:声明(您不应该使用),您对getline()
的调用不会调用 C++ 的std::getline()
,而是来自<stdio.h>
的 C 的getline()
:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Which is why you were getting errors about converting std::ifstream
to char**
.这就是为什么您在将std::ifstream
转换为char**
时遇到错误的原因。 To fix that error, make sure you use getline()
from the std
namespace, eg:要修复该错误,请确保使用std
命名空间中的getline()
,例如:
void createArray(player p[], std::ifstream &input){
for(int i =0;i < 10; i++){
std::getline(input, p[i].name, ' ');
...
}
}
However, std::getline()
can't read input into an int
, only into a std::string
.但是, std::getline()
不能将输入读入int
,只能读入std::string
。 If you want to read a formatted integer from text input, you will have to convert the std::string
using std::stoi()
or equivalent, eg:如果您想从文本输入中读取格式化的 integer,则必须使用std::stoi()
或等效项转换std::string
,例如:
void createArray(player p[], std::ifstream &input){
std::string temp;
for(int i =0;i < 10; i++){
...
std::getline(input, temp, ' ');
p[i].position = std::stoi(temp);
...
}
}
Otherwise, use operator>>
instead:否则,请改用operator>>
:
void createArray(player p[], std::ifstream &input){
for(int i =0;i < 10; i++){
input >> p[i].name >> p[i].position >> ...;
input.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.