开发者

no matching function for call when trying to use get function from fstream library

I'm getting two errors in main that have 开发者_如何转开发me stumped:

  1. "no matching function for call"
  2. "invalid initialization of non-const reference of type 'int&' from a temporary of type 'int*'"

Could anyone lend a hand? thanks!

header

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>

using namespace std;

void extern input(ifstream&, ofstream&, int&, int&);



#endif // HEADER_H_INCLUDED

main

#include "header.h"

using namespace std;

int main()
{
    int grade;
    int list[8];
    ifstream inData;
    ofstream outData;

    inData.open("Ch9_Ex4Data.txt");

    if (!inData)
    {
        cout << "Cannot open the input file."
             << endl;
            return 1;
    }

    outData.open("DataOut.txt");

    inData.get(grade); // << ERROR 1 HERE

    while (inData)
    {
        input(inData, outData, grade, list); // << ERROR 2 HERE
    }

    output (outData, list)

    return 0;
}


Error 1 is because inData.get() does not take an int, you should do

grade = inData.get();

and the second is because list is actually an int* and not an int so the fourth parameter in input() should be an int* and not an int&


An int[] is not an int&. An int& is a reference to an integer. int list[8] is an array of 8 integers. They are not the same thing.


For your first error, you're passing an int instead of a char or char*. See below for get prototypes:

istream::get

public member function
int get();
istream& get ( char& c );
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );
istream& get ( streambuf& sb);
istream& get ( streambuf& sb, char delim );

For the second error, you're wrongly passing an array of int where the function is expecting a int&.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜