no match for operator

Apr 18, 2013 at 2:37am
Hi, somebody please help me solve this, I get an error when compiler looks at while statement. (I've created the file and it is in the same directory, so it is reachable)I looked up online and did't find any solution either.

main.cpp:54: error: no match for 'operator>>' in 'rData >> S[n].main()::Student::name'



#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>
using namespace std;


int main()
{
int i = 0;
string line;
int meanGPA = 0;
int meanAge = 0;
int sum;




////////////////////////CREATE TWO STRUCTURES/////////////////////////////////
struct Date
{ int month;
int day;
int year;
} ;
struct Student
{ string name;
Date dob;
double GPA;
}S[200];

////////////////////////READ FROM THE FILE ////////////////////////////////////
ofstream rData;
rData.open ("student.txt", ios::out);


int n = 0;


while(rData >> S[n].name >> S[n].dob.day >>S[n].dob.month >> S[n].dob.year >> S[n].GPA) n++;


//////////////////////PROCESS ARRAY/////////////////////////////////////////////
for(;i < n;i++)
{
sum += S[i].GPA;
meanGPA = sum/n;

meanAge += 2013 - S[i].dob.year;
meanAge = meanAge/n;

}


cout << meanGPA << ' ' << meanAGE;
}
[/code]
Apr 18, 2013 at 2:49am
struct and class definitions must occur outside of function definitions:

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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>
using namespace std;

////////////////////////CREATE TWO STRUCTURES/////////////////////////////////
struct Date
{ int month;
int day;
int year;
} ;
struct Student
{ string name;
Date dob;
double GPA;
}S[200];


int main()
{
int i = 0;
string line;
int meanGPA = 0;
int meanAge = 0;
int sum;

////////////////////////READ FROM THE FILE ////////////////////////////////////
ofstream rData;
rData.open ("student.txt", ios::out);

int n = 0;

while(rData >> S[n].name >> S[n].dob.day >>S[n].dob.month >> S[n].dob.year >> S[n].GPA) n++;


//////////////////////PROCESS ARRAY/////////////////////////////////////////////
for(;i < n;i++)
{
sum += S[i].GPA;
meanGPA = sum/n;

meanAge += 2013 - S[i].dob.year;
meanAge = meanAge/n;

}


cout << meanGPA << ' ' << meanAGE;
}
Apr 18, 2013 at 2:56am
1
2
ofstream rData;
rData >> ...


The >> is an input operator. You can't use it on an ofstream.
Apr 18, 2013 at 2:59am
Thank you for your quick response. That was a silly mistake, but it doesn't work in this case either
Apr 18, 2013 at 3:06am
What is the new error then???

Also, you didn't initialize sum to 0, and the way you're calculating meanAge is totally wrong.
Topic archived. No new replies allowed.