运行编译器时,我不断收到这些错误:
Program04.cpp:21:14: error: variable or field âgetDataâ declared void
Program04.cpp:21:14: error: âStudentTypeâ was not declared in this scope
Program04.cpp:21:26: error: expected primary-expression before â]â token
Program04.cpp:21:29: error: expected primary-expression before âintâ
Program04.cpp:23:15: error: variable or field âsortDataâ declared void
Program04.cpp:23:15: error: âStudentTypeâ was not declared in this scope
Program04.cpp:23:27: error: expected primary-expression before â]â token
Program04.cpp:23:30: error: expected primary-expression before âintâ
Program04.cpp:27:22: error: variable or field âcomputeAveragesâ declared void
Program04.cpp:27:22: error: âStudentTypeâ was not declared in this scope
Program04.cpp:27:34: error: expected primary-expression before â]â token
Program04.cpp:27:37: error: expected primary-expression before âintâ
Program04.cpp:29:16: error: variable or field âprintDataâ declared void
Program04.cpp:29:16: error: âStudentTypeâ was not declared in this scope
Program04.cpp:29:28: error: expected primary-expression before â]â token
Program04.cpp:29:31: error: expected primary-expression before âintâ
Program04.cpp: In function âint main()â:
Program04.cpp:34:1: error: âStudentTypeâ was not declared in this scope
Program04.cpp:34:13: error: expected â;â before âstudentsâ
Program04.cpp:37:9: error: âstudentsâ was not declared in this scope
Program04.cpp:37:19: error: ânâ was not declared in this scope
Program04.cpp:37:20: error: âgetDataâ was not declared in this scope
Program04.cpp:38:21: error: âsortDataâ was not declared in this scope
Program04.cpp:39:25: error: âcomputeAveragesâ was not declared in this scope
Program04.cpp:40:19: error: âprintDataâ was not declared in this scope
Program04.cpp: At global scope:
Program04.cpp:45:14: error: variable or field âgetDataâ declared void
Program04.cpp:45:14: error: âStudentTypeâ was not declared in this scope
Program04.cpp:45:38: error: expected primary-expression before âintâ
一切对我来说都很好,所以我显然不明白。有什么建议么?
程序文件
using namespace std;
#include "StudentType.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>
#include <string>
const int MAX_STDS = 20;
void getData(StudentType[], int&);
void sortData(StudentType[], int&);
void getFormat(string&);
void computeAverages(StudentType[], int&);
void printData(StudentType[], int&);
int main () {
StudentType students[MAX_STDS];
StudentType();
getData(students, n);
sortData(students, n);
computeAverages(students);
printData(students);
return 0;
}
void getData(StudentType students[], int n){
ifstream fin;
int grade;
string filename, name;
bool done = false;
cout << "Enter filename: ";
cin >> filename;
fin.open(filename.c_str());
while(true) {
try {
fin.open(filename.c_str());
if(!fin) {
throw(string("Could not open " + filename + "."));
}
break;
}
catch (string s) {
cout << s << endl;
cout << "Enter a different file name: ";
cin >> filename;
}
}
n=0;
while(n<MAX_STDS && getline(fin, name)) {
students[n].setName(getFormat(name));
for(int i = 0; i < NUM_GRDS; ++i) {
fin >> grade;
student[n].setGrade(grade, i);
}
getline(fin, name);
++n;
}
}
void printData(StudentType students[], int n) {
for(int i = 0; i < n; ++i) {
students[i].printLine();
}
}
void computeAverages(StudentType students[], int n) {
for(int i = 0; i < n; ++i) {
students[i].computeAverage();
}
}
void sortData(StudentType students[], int n) {
for(int i=0; i<n-1; i++) {
for(int j=0; j < n-1-i; ++j) {
if(students[j].getName() > students[j+1].getName()) {
swap(students[j], students[j+1]);
}
}
}
}
void getFormat(string& name) {
string first;
string middle;
string last;
char n, m;
int size = 0;
n = name.find(' ');
first = name.substr(0, n);
m = name.find(' ', n + 1);
size = name.size();
if (m != string::npos) {
middle = name.substr(n+1, m-(n+1));
last = name.substr(m+1, size - (m+1));
}
else {
middle = "";
last = name.substr(n + 1, size - (n + 1));
}
name = last + ", " + first;
if (middle != "") {
name = (name + ' ') + middle[0];
}
}
头文件
#ifdef STUDENTTYPE__H
#define STUDENTTYPE__H
#include <string>
#include<iostream>
const int NUM_GRDS = 10;
class StudentType {
public:
StudentType();
void setName(std::string);
void setGrade(int, int);
void computeAverage();
std::string getName() const;
void print(std::ostream& = std::cout) const;
private:
std::string name;
int grades[NUM_GRDS];
float avg;
};
#endif
实施文件
#include "StudentType.h"
#include <iomanip>
StudentType::StudentType(){
name = "";
for(int i =0; i <NUM_GRDS; ++i){
grades[i] = 0;
}
avg = 0.0;
}
void StudentType::setName(std::string newName){
name = newName;
}
void StudentType::setGrade(int grade, int num){
grades[num] = grade;
}
void StudentType::computeAverage(){
float total = 0;
for(int i = 0; i<NUM_GRDS; ++i){
total += grades[i];
}
avg = total/NUM_GRDS;
}
std::string StudentType::getName() const{
return name;
}
void StudentType::printLine(std::ostream& out) const {
out.setf(std::left);
out.setf(std::fixed);
out.setf(std::showpoint);
out << "\n" << setw(25) << "Student" << setw(50) << "Grades" << setw(10) << "Average" << endl;
out << "_____________________________________________________________________________________" << endl;
out << std::left << std::setw(25) << name << std::right << ' ';
for(int i = 0; i < NUM_GRDS; ++i){
out << std::setw(5) << grades[i] << ' ';
}
out << setprecision(2) << std::setw(6) << avg << endl;
}
程序编译后我的输出应该是这样的......
Enter file name: grades.dat
Student Grades Average
________________________________________________________________________________________
Last, First 90 80 70 60 50 40 30 20 10 0 45.00
Last, First 40 40 40 40 40 40 40 40 40 40 40.00
Last, First 54 98 65 32 21 87 54 65 98 32 60.60
Flames, Blood A 9 8 7 6 5 4 3 2 1 0 4.50
Bottoms, Car 32 65 98 87 54 24 56 89 78 68 65.10
Guitars, Dean 10 10 10 10 10 10 10 10 10 10 10.00
Honer, Ruth T 78 56 12 23 45 89 31 64 97 79 57.40
Hot, Zepher R 12 54 87 89 56 32 51 46 97 31 55.50
.
.
.
输入文件应具有此格式并包含 20 多个学生用于测试目的:g0、g1、...g9 应为 0 到 100 的 10 个等级
First Middle Last
g0 g1 g2 g3 g4 g5 g6 g7 g8 g9
First Last
g0 g1 g2 g3 g4 g5 g6 g7 g8 g9