getline();使用C++时出错

getline(); error using C++

本文关键字:C++ 出错 使用 getline      更新时间:2023-10-16

当我尝试运行以下程序时,我从getline()调用中得到以下错误。

消息是:

In function 'int main()':
error: no matching function for call to 'getline(std::ofstream&, std::string&)'

我不知道我为什么得到这个——我有string库。

我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    int NumeroLetras;
    cout << "Juego de El Ahorcadon--------------------n" << endl;
    cout << "N243mero de letras de la palabra: ";
    cin >> NumeroLetras;
/*                Creamos el fichero con las palabras a adivinar
------------------------------------------------------------------------*/
    cout << "Creando fichero con palabras..." << endl;
    ofstream fichero("palabras.txt");
    fichero << "balonceston";
    fichero << "beisboln";
    fichero << "futboln";
    fichero << "golfn";
    fichero << "rugbyn";
    fichero << "tenisn";
    fichero << "boxeon";
    fichero << "sumon";
    fichero << "judon";
    fichero << "nascarn";
    fichero << "atletismon";
    fichero << "caminatan";
    fichero << "ciclismon";
    fichero << "esgriman";
    fichero << "natacionn";
    fichero << "polon";
    fichero << "clavadosn";
    fichero << "remon";
    fichero << "velan";
    fichero << "ajedrezn";
    fichero.close();
    cout << "Fichero creado exitosamente..." << endl;
/*                Determinamos el tamaño de las palabras
------------------------------------------------------------------------*/
    string Palabra;
    fichero.open("palabras.txt");
    while (! fichero.eof())
    {
        getline(fichero, Palabra);
        cout << Palabra << endl;
    }
    return 0;
}

为什么我会出现这个错误?

您正试图从ofstream输出文件流)读取

创建一个新的ifstream变量并从中读取。