variables - 无法编译::I don't understand my errors. Mo

运行编译器时,我不断收到这些错误:

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应该是10个等级,范围从0到100

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

最佳答案

更改

#ifdef STUDENTTYPE__H


#ifndef STUDENTTYPE__H

那么你有适当的双重包容护卫

(我必须说很常见的错字)

关于variables - 无法编译::I don't understand my errors. MoSTLy the “declared as void” errors and “StudentType was not declared in this scope” .,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15759294/

相关文章:

vba - VB-下标超出范围,错误9

php - 在XPath评估之前,如何处理字符串中的双引号?

java - 二进制搜索静态方法遇到问题无法引用

java - 在 int 上执行方法

macos - 在MacOS上编译Irrlicht项目时出现“symbol(s) not found

c++ - 在 ns 2.34 (Jiazi YI ns 2.29) 上添加 mp-olsr 时出现

java - Double不是抽象的,并且不会重写ListInterface中的抽象方法getNex

arrays - 数组表达式语法错误

java - 引用从对象数组进行打印的方法时找不到符号错误

compiler-errors - JRE 抛出的 fatal error