【发布时间】:2021-03-30 13:59:26
【问题描述】:
我是 C++ 新手,我的代码在这里遇到了问题:
#include <iostream>
using namespace std;
class Student {
public:
string student_name;
double CGPA;
string degree;
static unsigned int count_total_no_of_students_enrolled; //STATIC VARIABLE
const string uni_name = "umt";
void setstudent(string a, double b, string c);
void displaystudent();
void display_total_no_of_student();
};
void Student::setstudent(string a, double b, string c) {
student_name = a;
CGPA = b;
degree = c;
count_total_no_of_students_enrolled++; //******THIS ISN'T WORKING******
}
void Student::displaystudent() {
cout << "\n=> Student details:\n";
cout << " Name: "<< student_name << ",CGPA: " << CGPA << "\n Degree: " << degree << ",University: " << uni_name;
}
void Student::display_total_no_of_student() {
cout << "\n Total Students Enrolled: " << count_total_no_of_students_enrolled;
}
int main() {
Student s1, s2, s3;
s1.setstudent("John Doe", 3.5 , "CS");
s2.setstudent("Jane Doe", 3.9 , "CS");
s3.setstudent("Jim Doe", 3.8, "CA");
s1.displaystudent(); s2.displaystudent(); s3.displaystudent();
display_total_no_of_student(); //*****THIS GIVES ERROR TOO*****
return 0;
}
除了static 变量外,一切都按预期工作。每次创建 Student 类的新对象时,我想将其值增加 1。非常感谢您提供的任何帮助
【问题讨论】: