【问题标题】:Getting "Expected an identifier" and "missing ';' before '.'" in class with a struct得到“期望一个标识符”和“缺少';'在“。”之前在类中使用结构
【发布时间】: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 之前。

标签: c++ class struct


【解决方案1】:

变化:

book_entry.get_student(string name);

book_entry.get_student(name);

另外,book_entry 应该是此范围内可用的对象。您显示的代码没有它,而是您的第一个代码 sn-p 说它是 type 而不是对象。

【讨论】:

  • 是的。这是一个问题。
  • 我做了那个改变,它在name下划线,说它不是一个类型名称。至于你提出的第二个问题,我该如何改变?
  • 这是该行的正确修复。看看你是否得到编译器错误。您可能对智能感知感到困惑。
猜你喜欢
  • 1970-01-01
  • 2014-09-02
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多