3 Replies - 421 Views - Last Post: 02 May 2011 - 07:27 AM Rate Topic: -----

#1 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

error trying to output with classes

Posted 01 May 2011 - 10:22 PM

Im trying to read from a file using classes and objects. i keep getting this error


main.cpp:16: error: no matching function for call to ‘Game::fillGame()’
Game.h:41: note: candidates are: bool Game::fillGame(std::ifstream&)


here is code that goes with the error


//Game.h

 bool fillGame(ifstream & din);






//Game.cpp

#include<iostream>
#include "Game.h"

bool Game::fillGame(ifstream &din)
{
int round_Of;
ifstream din;


    din.open("file.txt");
    if(!din)
        cout << "Error: file could not be found";
    else
    {
        din >> round_Of;
        while(!din.eof())
        {
            cout  << round_Of;
            din >> round_Of;
        }
        cout << endl << endl;
        din.close();
    }

}




//main.cpp

#include "Game.h"


#include<iostream>
#include<cstring>
#include<iomanip>
#include<cctype>
#include<fstream>
#include<string>

int main()
{
    Game output;
    output.fillGame();





    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: error trying to output with classes

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: error trying to output with classes

Posted 01 May 2011 - 11:19 PM

Hi,

Reading error messages is one of the many skills needed to successfully fault find a problem.

Where the message stated "can't find" it means the signature of the function cannot be found. Looking at your declaration in the class, the compiler expects to find a match with the one you created in main.

However, there isn't a matching signature, hence the error. To fix, you must amend the call in main to include the stream reference to use with the function.
{
15	    Game output;
16	    output.fillGame(<<-- missing the reference needed here -->>);


Good luck
Was This Post Helpful? 1
  • +
  • -

#3 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: error trying to output with classes

Posted 02 May 2011 - 12:01 AM

View PostgregoryH, on 01 May 2011 - 11:19 PM, said:

Hi,

Reading error messages is one of the many skills needed to successfully fault find a problem.

Where the message stated "can't find" it means the signature of the function cannot be found. Looking at your declaration in the class, the compiler expects to find a match with the one you created in main.

However, there isn't a matching signature, hence the error. To fix, you must amend the call in main to include the stream reference to use with the function.
{
15	    Game output;
16	    output.fillGame(<<-- missing the reference needed here -->>);


Good luck





I'm assuming your talking about something like this. when i do that i get the errors below the code

int main()
{
    Game output;
    output.fillGame(ifstream );





    return 0;
}




main.cpp:16: error: expected primary-expression before ‘)’ token
In file included from main.cpp:4:
Game.cpp:7: error: declaration of ‘std::ifstream din’ shadows a parameter
Game.cpp:7: error: aggregate ‘std::ifstream din’ has incomplete type and cannot be defined


Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: error trying to output with classes

Posted 02 May 2011 - 07:27 AM

Hi Again

The process of using classes generally requires a class to be instantiated (unless its a static class). ifstream must be instantiated by creating a variable, which is used to open the file (through a public member).

This sample shows how to create (instantiate) the varialble of type ifstream and open a file in the in mode:

ifstream infile;

  infile.open ("test.txt", ifstream::in);



Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1