-3

これは私が得ているエラーです:

ambiguous overload for ‘operator>>’ in ‘contestantsInputFile >> contestantName’|

名前を競技者名という変数に読み込むために、参照によってファイルを関数に渡そうとしています。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string contestantName = "";
string contestantName(ifstream &);

int main()
{
    ifstream contestantsInputFile;
    contestantsInputFile.open("contestants_file.txt");    
    contestantName(contestantsInputFile);
}


string contestantName(ifstream &contestantsInputFile)
{
    contestantsInputFile >> contestantName; //this is the line with the error
    return contestantName;
}
4

1 に答える 1

1

std::istream から関数を読み込もうとしています:

contestantsInputFile >> contestantName; //this is the line with the error

たぶん、これはあなたが意図していたものです(テストされていません):

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string readContestantName(ifstream &); // renamed to be a descriptive verb

int main()
{
    ifstream contestantsInputFile;
    contestantsInputFile.open("contestants_file.txt");    
    std::string contestantName = readContestantName(contestantsInputFile);

    std::cout << "contestant: " << contestantName << "\n";
}


string readContestantName(ifstream &contestantsInputFile)
{
    std::string contestantName; // new variable
    contestantsInputFile >> contestantName; //this is the line with the error
    return contestantName;
}
于 2013-10-03T22:06:34.390 に答える