【发布时间】:2013-09-22 12:54:46
【问题描述】:
我正在尝试制作一个成绩册程序。我在为成绩簿定义一个类的早期阶段,它是结构的向量,每个结构都有一个学生姓名的字符串和一个学生成绩的向量。这是gradebook.h 标头:
// Creates a gradebook database that can be accessed and modified
#ifndef GRADEBOOK_H
#define GRADEBOOK_H
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
class gradebook {
public:
//--constructors
gradebook();
// post: An empty database is constructed
//--modifiers
void add_student(string name);
// post: A new student is added who has the given name
bool remove_student(string name);
// post: If the name matches an existing student, that student is removed.
// Otherwise, return false.
void add_grades();
// post: Adds grades input from the keyboard
//--accessors
void show_total_grade(string name);
// post: Displays the cumulative grade for the student. If no student by
// that name, display a message conveying such.
void show_class_average();
// post: Displays the average grade for the class.
void show_student_grade(string name);
// post: Displays grades for all assignemnts for the given student.
void show_assignment_grade(int assignment);
// post: Displays all the grades for the given assignment number.
//--iterator functions
struct book_entry
{
string my_name;
vector<double> grades;
book_entry(string name)
{
my_name = name;
vector<double> grades;
}
void get_student(string name) {
cout << my_name << ": ";
for (auto &i : grades)
cout << i << " ";
cout << endl;
}
};
private:
string my_student_name;
string my_assignment;
vector<book_entry> my_book;
double my_grade;
};
#endif
以及实现:
#include "gradebook.h"
#include <vector>
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::cin;
using std::vector;
//--constructors
gradebook::gradebook() {
vector<book_entry> my_book;
}
//--modifiers
void gradebook::add_student(string name) {
//my_book.next() =
}
//--accessors
void gradebook::show_student_grade(string name) {
book_entry.get_student(string name);
}
void gradebook::show_assignment_grade(int assignment) {
}
//--iterator functions
我使用的是 MSVS 2013,当我构建项目时,我在实现的第 23 行收到错误 (book_entry.get_student(string name);)。错误是“缺少';'前 '。'”。该行中的句点有一个波浪形的红色下划线,如果我将鼠标悬停在它上面,我会得到一个不同的错误:“需要一个标识符”。看来我误解了如何使用我设置的结构。我该如何解决这个问题?
【问题讨论】:
-
我该如何解决这个问题?最后一个花括号后有一个分号。
-
我有两个;一个在结构的末尾,就在私有数据部分之前,另一个在之后,在
#endif之前。