c++ - 表达式必须有类类型(学生数据库)?

标签 c++

我对 C++ 编程还很陌生,我一直在尝试制作一个学生数据库程序,其中已完成类(class)的分数将按升序排序。 这里棘手的部分是我在类中声明的变量必须保持私有(private)。在我的排序函数中使用变量显示错误,指出表达式必须具有类。我真的不知道解决这个问题的方法是什么。任何帮助是极大的赞赏! :) 代码是`

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Student{
private:
    int data[100];
    string name;
    string id;
        float cgpa, hcgpa, lcgpa;
        int marks[100];
    int cc; //courses Completed

public:
    int getMarks(void){
        int i = 100;
        int m; 
        return m = marks[i];

    }
    void getInfo(int i){
        cout << "Student " << i << ": " << endl;

        cout << "ID: ";
        cin >> id;
        cin.ignore();

        cout << "Name: ";
        getline(cin, name);
        cin.ignore();
        cout << "CGPA: ";
        cin >> cgpa;
        cout << "Number of courses completed: ";
        cin >> cc;
        cout << "Please enter the marks respectively: " << endl;
        for (int j = 1; j <= cc; j++){
            cout << "Mark " << j << ": " << endl;
            cin >> marks[j];

        }

        cout << endl;
    }
    void sortMarks(Student z[], int i, int &n){
        int j;
        for (i = 0; i < n-1; i++){
            j = i;      
            while (j>0 && z[j - 1].getMarks > z[j].getMarks){
                swap(z.[j - 1]getMarks(), z.[j]getMarks());
                j--;
            }
        }
    }

    void showInfo(int i){
        cout << "Student " << i << ": " << endl;
        cout << "ID: " << id << endl;
        cout << "Name: " << name << endl;
        cout << "CGPA: " << cgpa << endl;
        cout << "Number of courses completed: " << cc << endl;
        cout << endl;
        for (int j = 1; j <= cc; j++){
            cout << "Marks for course " << j << ": " << endl;
            cout << marks[j] << endl;

        }
    }
};
int main(){
    Student s[100];
    int no; //should be less than 100

    cout << "Number of students: ";
    cin >> no;
    cout << endl;

    for (int i = 1; i <= no; i++) {
        s[i].getInfo(i);
    }
    for (int i = 1; i <= no; i++) {
        s[i].showInfo(i);
    }
    cout << "After sort: \n" << endl;
    for (int i = 1; i <= no; i++) {
        s[i].sortMarks(i);
        s[i].showInfo(i);
    }

    system("pause");
    //Error is it doesn't show the exact values after the first student's info.
}

`

最佳答案

也许你的意思是

swap(z[j - 1].getMarks(), z[j].getMarks());

代替

swap(z.[j - 1]getMarks(), z.[j]getMarks());

尽管此构造没有任何意义,因为编译器在任何情况下都会发出错误,因为您正在尝试交换 getMarks 返回的临时对象。

这个成员函数

int getMarks(void){
    int i = 100;
    int m; 
    return m = marks[i];

}

根本无效,没有任何意义。

所以你的代码的问题不在错误中。问题是您的代码整体无效。

例如函数 sortMarks 应该定义为静态成员函数。您可以在该函数中直接访问标记数组的元素,而无需使用 getMarks。

关于c++ - 表达式必须有类类型(学生数据库)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24743656/

相关文章:

c++ - 为什么我不能在 linux 的代码块中使用 "auto"

c++ - 读取一个值并使用它而不用声明一个额外的变量,就像在 Java 的 sc.next() 中一样

c++ - 模板 (C++) - 不确定是否正确

C++:如何在类和基元上使用模板?

c++ - 如何安装系统范围的 nuget 包?

c++ - 如何将一个空 vector 提升为一个默认元素的 vector ?

c++ - 高级优先队列

c++ - 如何将 OpenCV Mat 转换为 Halide Image 并返回?

平台之间的 C++ 正则表达式差异

c++ - OpenCV:为什么 Haar 分类器只检测到一张脸和一只眼睛?