简体   繁体   English

错误:无法转换 'std::ifstream {aka std::basic_ifstream<char> }' to 'char**' for getline() function</char>

[英]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.

相关问题 无法转换 'std::ifstream' {aka 'std::basic_ifstream<char> '} 到 'bool' 作为回报</char> - Cannot convert 'std::ifstream' {aka 'std::basic_ifstream<char>'} to 'bool' in return 错误:&#39;operator&lt;&lt;&#39; 不匹配(操作数类型是 &#39;std::ifstream&#39; {aka &#39;std::basic_ifstream<char> &#39;} 和 &#39;CHAR [39]&#39; {aka &#39;char [39]&#39;})| - error: no match for 'operator<<' (operand types are 'std::ifstream' {aka 'std::basic_ifstream<char>'} and 'CHAR [39]' {aka 'char [39]'})| 错误:没有匹配的函数调用&#39;std::basic_ifstream<char> ::basic_ifstream(std::__cxx11::string&amp;)&#39; - error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)' 没有用于调用 &#39;std::basic_ifstream 的匹配函数<char> ::basic_ifstream(QString&amp;)&#39; - no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(QString&)’ 没有匹配的 function 调用 'std::basic_ifstream<char> ::basic_ifstream(std::__cxx11::string&amp;)' ifstream myfile(filename);</char> - no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)’ ifstream myfile(filename); 没有匹配的函数调用std :: basic_ifstream - No matching function for call to std::basic_ifstream ifstream的std :: getline,使用参数字符串或char * - std::getline for an ifstream, using parameter string or char * 为什么`std::basic_ifstream<char16_t> ` 在 C++11 中不起作用? - Why does `std::basic_ifstream<char16_t>` not work in c++11? 我一直从Xcode收到一个错误,提示“ Apple Mach-O Linker(Id)Error” readIt(std :: _ 1 :: basic_ifstream <char,std::_1::char_traits<char> &gt;&,” - I keep getting an error from Xcode that says “ Apple Mach-O Linker (Id) Error ”readIt(std::_1::basic_ifstream<char,std::_1::char_traits<char> >&," 在std :: basic_ifstream上编译C ++错误 - C++ compile error on std::basic_ifstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM