trouble with classes

for some reason im having trouble getting my program to even compile now i will include the errors and the header and body files for reference


thanks


game.h:11: error: âifstreamâ has not been declared
game.h:12: error: expected â,â or â...â before âFilenameâ
game.h:12: error: ISO C++ forbids declaration of âstringâ with no type
game.h:17: error: âstringâ does not name a type




#include <fstream>
#include <string>
class Game

{
public:
Game();
Game (const Game &OtherGame);
~Game();

void GetData(ifstream &Input);
void Fill(const string Filename);
void Print() const;
private:
int games;
int numgames;
string rival;
int HomeScore;
int RivalScore;
};





#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include "game.h"
using namespace std;

Game::Game()
{

HomeScore = 0;
RivalScore = 0;
}

Game::Game (const Game &OtherGame)
{
rival = OtherGame.rival;
RivalScore = OtherGame.RivalScore;
HomeScore = OtherGame.HomeScore;
}

Game::~Game()
{
}

void Game::GetData(ifstream &Input)
{

}

void Game::Fill (const string Filename)
{
Din.open(Filename.c_str());
if (!Din)
{
success = false;
}
else
{
while(games[numgames].ReadData(Din))
{
numgames++;
}

}

void Game::Print() const
{
for (int i = 0; i < numgames; i++)
{
games[i].Print();
}

}


the 'game.h:17:' error refers to the face that you have delcared using namespace std; after you have tried to declare a string variable. For this to work you would have to write std::string. But you should just move that line to the very top and keep all includes and using's together, or probably just not use using

Also, don't think (ifstream &input) is valid! You can't have ifstream as the parameter type!
You can make anything (take with a grain of salt) a parameter.
ok now the only problem i am getting is

hw7.cpp:12: error: no matching function for call to âGame::GetData(const char [9])â
game.h:12: note: candidates are: void Game::GetData(std::ifstream&)


but here are the 2 lines it says is the problem

void GetData(ifstream &Input);
void Game::GetData(ifstream &Input)

so i dont understand why its not taking the input when they are the exact same parameter
Did you move the rest of your #includes up to the top of the file?
alright i fixed all the previous probems and have a problem i have never encountered
1
2
game.cpp: In member function âvoid Game::Fill(std::string)â:
game.cpp:53: error: request for member âGetDataâ in â((Game*)this)->Game::games[((Game*)this)->Game::numgames]â, which is of non-class type âintâ


and the function the line is in is the
while(games[numgames].GetData(Din))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include "game.h"
using namespace std;

Game::Game()
{

   HomeScore = 0;
   RivalScore = 0;
}

Game::Game (const Game &OtherGame)
{
   rival = OtherGame.rival;
   RivalScore = OtherGame.RivalScore;
   HomeScore = OtherGame.HomeScore;
}

Game::~Game()
{
}

void Game::GetData(ifstream &Input)
{
bool success;
  if(Input>>rival>>RivalScore>>HomeScore)
         success = true;
  else
         success = false;

}

void Game::SetInfo(const string Opponent, const int OScore, const int HScore)
{
rival = Opponent;
RivalScore = OScore;
HomeScore = HScore;
}
void Game::Fill (const string Filename)
{
ifstream Din;
 Din.open(Filename.c_str());
   if (!Din)     
   {                
        success = false;
   }        
   else
   {
      while(games[numgames].GetData(Din))
      {           
         numgames++;
      }
   }
}

void Game::Print() const             
{
for (int i = 0; i < numgames; i++)
   {
        games[i];
   }

}
Topic archived. No new replies allowed.