file io error

I have no idea what is wrong with this function i keep getting an invalid operator for >>. all im doing is inputting data into an object from a file


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
ifstream Din;
   cout << "do you want to load monster from a file? enter Y to continue";
   cin >> load;
   if (load == 'Y' || load == 'y')
   {ifstream Din;
 
   Din.open("monster.txt");
   if (!Din)
   {
	cout << "Error!";
   }
   else
       readFile(monster1,Din);
       readFile(monster2,Din);
       readFile(monster3,Din);
       readFile(monster4,Din);
       readFile(monster5,Din);
       readFile(monster6,Din);
       readFile(monster7,Din);
       readFile(monster8,Din);
       Din.close();
   }



void readFile(Monster M, ifstream & Din)
{
    Din >> M.getType() >> M.getPower() >> M.getGold() << endl;
}


/cygdrive/I/programming/ListDemo.cpp: In function `void readFile(Monster, std::ifstream&)':
/cygdrive/I/programming/ListDemo.cpp:133: error: no match for 'operator>>' in 'Din >> Monster::getType()()'
Last edited on
You have two ifstream Din. Remove the first one.
Pass Monster M as a reference, otherwise you're changing an object which get's destructed at the end of readFile().
Put brackets around the readFile() calls.
Use an array (or vector) of objects instead of 8 distinct variable.
Why did you put << endl after the inputs? Remove it.
What does Monster::getType(), Monster::getPower() and Monster::getGold() return? If not references, then change those also. That's all for now.
Topic archived. No new replies allowed.