3 Attachment(s)
Problem with ostream, error: "undefined symbol 'out' in function main"
This program is our first using classes, and I cannot figure out a compiler error. Any help is really appreciated, as this may decide whether I pass the class. :-/
The program is designed to input a set of a grades from a data file, average the grades, and return the name of the student, the student's grades, and the student's average.
The only problem is one compiler error in line 24 of Program03.cpp. I'm using the Borland Compiler, and the error I get states:
"Undefined symbol 'out' in function main".
I've tried declaring 'out' but I can't seem to get it correct. Any idea why I'm getting this? I'm copying(what I believe are) the relevant parts of the three files below.
Code:
//Program03.cpp
#include "StudentType.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_STUDENTS = 10;
int main(){
string filename;
ifstream fin;
StudentType students[NUM_STUDENTS];
cout << "Enter filename: ";
cin >> filename;
fin.open(filename.c_str());
if(fin){
cout << "File successfully opened." << endl;
for(int i = 0; i < NUM_STUDENTS; ++i){
students[i].getData(fin);
students[i].printData(out); //This is where the error is.}
}
else{
cout << "File failed." << endl;
cout << filename << " could not be opened. \n";
}
return 0;
}
Code:
//StudentType.cpp
#include "StudentType.h"
using namespace std;
StudentType::StudentType(){
name = " ";
average = 0.0;
for(int i = 0; i < NUM_GRADES; ++i){
grades[i] = 0;
}
}
void StudentType::getData(std::ifstream& fin){
float total = 0.0;
fin >> name;
for(int i =0; i < NUM_GRADES; ++i){
fin >> grades[i];
total += grades[i];
}
average = total/NUM_GRADES;
}
void StudentType::printData(std::ostream& out) const
{
cout << name;
for(int i = 0; i < NUM_GRADES; ++i){
out << grades[i];
}
out << average << endl;
}
Code:
//StudentType.h
#ifndef STUDENTTYPE_H
#define STUDENTTYPE_H
#include <string>
#include <iostream>
#include <fstream>
const int NUM_GRADES = 10;
class StudentType{
public:
StudentType();
void getData(std::ifstream&);
void printData(std::ostream&) const;
private:
std::string name;
int grades[NUM_GRADES];
float average;
};
#endif