简体   繁体   English

错误:没有用于调用“getline(FILE*&, std::string&)”的匹配函数

[英]error: no matching function for call to 'getline(FILE*&, std::string&)'

error: no matching function for call to 'getline(FILE*&, std::string&)' Function code:错误:没有匹配的函数调用'getline(FILE*&, std::string&)' 函数代码:

void CarregarArquivo(){
aluno alunos_auxiliar[MAX];
FILE *arquivo;
arquivo=fopen ("texto.txt","r");
int quantidade=0;
fscanf(arquivo,"%d",&quantidade);
if(quantidade!=0){
    quantusuario=quantidade;
    for(int i=0;i<quantidade;i++){
        getline(arquivo,alunos[i].nome);
        fscanf(arquivo,"%d",&alunos[i].matricula);
        printf("%d",alunos[i].matricula);
        fscanf(arquivo,"%d/%d/%d",&alunos[i].nascimento.dia,&alunos[i].nascimento.mes,&alunos[i].nascimento.ano);
        if(alunos[i].numero!=0){
            for(int j=0;j<alunos[i].numero;j++){
                getline(arquivo,alunos[i].materias[j].nome);
                fscanf(arquivo,"%.1f",&alunos[i].materias[j].nota);
            }
        }
    }
}
else if(quantidade == 0 && arquivo == NULL){
    quantusuario =0;
}
fclose(arquivo);

} }

includes:包括:

registro.h has structs in there: registro.h 中有结构:

struct aluno{
string nome;
int numero;
int matricula;
data nascimento;
disciplina materias[10];

}; };

includes:包括:

#include"registros.h"
#define MAX 100
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"funcoes.h"
#include <fstream>
#include<string>

Getline please work. Getline 请工作。

I'm Brazilian.Ignore the name of variables.我是巴西人。忽略变量的名称。 Sorry about my English.对不起我的英语。

You are mixing up the C library's FILE * functions, and C++ library functions that use std::istream .您正在混淆 C 库的FILE *函数和使用std::istream C++ 库函数。

You need to rewrite your code and replace all usage of FILE * , including fopen() , et al, with std::ifstream .您需要重写代码并用std::ifstream替换FILE *所有用法,包括fopen()等。

The first parameter to std::getline is a std::istream & , and not a FILE * . std::getline的第一个参数是std::istream & ,而不是FILE *

I think you are confusing the C and C++ API's.我认为您混淆了 C 和 C++ API。 In C++, this is the function:在 C++ 中,这是函数:

http://www.cplusplus.com/reference/string/string/getline/ http://www.cplusplus.com/reference/string/string/getline/

std::getline(std::istream&, std::string&)

whereas in C, the function is indeed而在 C 中,函数确实是

http://man7.org/linux/man-pages/man3/getline.3.html http://man7.org/linux/man-pages/man3/getline.3.html

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

Pick one and stick to it!选择一个并坚持下去! If you choose C++, then use std::fstream instead of the FILE* and so on.如果您选择 C++,则使用std::fstream而不是FILE*等等。 Remember to prepend std:: or use using namespace std;记得在std::前面加上using namespace std; to avoid errors.以免出错。

So it would start like this:所以它会像这样开始:

std::ifstream arquivo("texto.txt");
int quantidade=0;
arquivo >> quantidade;
if(quantidade!=0){
    quantusuario=quantidade;
    for(int i=0;i<quantidade;i++){
        std::getline(arquivo,alunos[i].nome);
        arquivo >> alunos[i].matricula;

And so on.等等。 Note how you already are using std::string , so your choice is already made.请注意您已经如何使用std::string ,因此您的选择已经做出。

thx guys it compiles now. thx 伙计们它现在编译。 I put like this:我是这样写的:

void CarregarArquivo(){
//FILE *arquivo;
//arquivo=fopen ("texto.txt","r");
int quantidade=0;
ifstream arquivo("texto.txt");
char barra=' ';
arquivo >> quantidade;
if(quantidade!=0){
    quantusuario=quantidade;

    for(int i=0;i<quantidade;i++){
        getline(arquivo,alunos[i].nome);
        cout << alunos[i].nome;
        arquivo >> alunos[i].matricula;
        arquivo >> alunos[i].nascimento.dia;
        arquivo >> barra;
        arquivo >> alunos[i].nascimento.mes;
        arquivo >> barra;
        arquivo >> alunos[i].nascimento.ano;
        if(alunos[i].numero!=0){
            for(int j=0;j<alunos[i].numero;j++){
                getline(arquivo,alunos[i].materias[j].nome);
                arquivo >> alunos[i].materias[j].nota;
            }
        }
    }
}
else if(quantidade == 0 && arquivo == NULL){
    quantusuario =0;
}
arquivo.close();
}

"barra" is because my txt have an date with this format: xx/xx/xxxx.now all i need is Getline working fine. “barra”是因为我的 txt 有一个这种格式的日期:xx/xx/xxxx。现在我需要的是 Getline 工作正常。 :(.It is not working yet. My txt is like that: :(.它还没有工作。我的txt是这样的:

2
Thiago
12312
21/12/1995
Math
10.0
Scott
31233
12/12/1995

First is the number of peoples a need to read.首先是需要阅读的人数。 Next is registration and date of birth.The cout bellow getline is just for test and it printf nothing.iksemyonov i'm using using namespace std.接下来是注册和出生日期。cout bellow getline 仅用于测试,它 printf nothing.iksemyonov 我正在使用命名空间 std。 Edit: The troubles are from formation of .txt,thx guys编辑:麻烦来自 .txt 的形成,thx 伙计们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 getline 给出错误:没有匹配的 function 调用 'getline(std::istream&amp;, const string&amp;)' - using getline gives error: no matching function for call to ‘getline(std::istream&, const string&)’ 错误:没有匹配函数来调用...(std :: string&) - error: no matching function for call to…(std::string&) 为什么C ++中的getline()无法正常工作? (没有匹配函数可调用“ getline(std :: ofstream&,std :: string&)” - Why is getline() in C++ not working? (no matching function for call to 'getline(std::ofstream&, std::string&)' 错误:没有匹配函数可调用&#39;isupper(std :: string&)&#39;| - error: no matching function for call to 'isupper(std::string&)'| 字符串的排列-错误:没有匹配的函数来调用&#39;std :: set <char> :: insert(std :: string&)&#39; - Permutations of a string --error: no matching function for call to ‘std::set<char>::insert(std::string&)’ 为什么这个编译错误? - 没有匹配函数来调用&#39;std :: basic_ofstream <char> ::打开(的std :: string&)” - why this compiler error? - no matching function for call to 'std::basic_ofstream<char>::open(std::string&)' 错误:没有匹配的函数调用&#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&)' “没有匹配函数来调用'async(std :: launch,<unresolved overloaded function type>,std :: string&)'” - “no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’” 没有匹配的 function 调用 'std::vector::push_back(std::string&amp;)' - No matching function for call to ‘std::vector::push_back(std::string&)’ 没有匹配的 function 调用 'LiquidCrystal::write(String&amp;)' - no matching function for call to 'LiquidCrystal::write(String&)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM