初始化和递增静态成员变量时出现问题

Trouble initializing and incrementing a static member variable

我是 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 class 的新对象时,我都想将其值增加 1。非常感谢您提供的任何帮助

需要初始化静态变量。在声明外初始化静态变量。

静态数据成员在其 class 定义中的声明不是定义,可能是除 cv 限定的 void 之外的不完整类型。静态数据成员的定义应出现在包含成员的 class 定义的命名空间范围内。在名称空间范围的定义中,静态数据成员的名称应使用 :: 运算符由其 class 名称限定。静态数据成员定义中的初始化表达式在其class(3.3.7).

的范围内

您的代码中的两个错误是:

  1. 您对 display_total_no_of_student() 的调用没有为该函数提供 class 对象或 class 名称,因此编译器正在寻找 'free' name/signature 的函数,它找不到。

  2. 尽管您为静态 count_total_no_of_students_enrolled 成员提供了 声明,但您还没有给出它的实际定义(必须是在 class 定义之外完成。

对于第一个问题,您很可能希望将 display_total_no_of_student 声明为 static 函数(因为它不使用或不需要任何 特定实例Student class)。对于第二个问题,只需提供count_total_no_of_students_enrolled成员的定义,该成员应初始化为零。

这是您的代码的 'working' 版本,解决了以下问题:

#include <iostream>

using namespace std;

class Student {
public:
    string student_name;
    double CGPA;
    string degree;
    static unsigned int count_total_no_of_students_enrolled; // This DECLARES the variable but doesn't define it!
    const string uni_name = "umt";

    void setstudent(string a, double b, string c);
    void displaystudent();
    static void display_total_no_of_student(); // Declare this function as static!
};

unsigned int Student::count_total_no_of_students_enrolled = 0; // This is the REQUIRED definition and initial value!

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();

    Student::display_total_no_of_student(); // Specify the class name to access a static member function!

    return 0;
}

如有任何进一步的说明,请随时提出要求and/or。

扩展阿德里安关于 static 的回答:

您只需在 class 中声明 static 变量 count_total_no_of_students_enrolled - 您需要提供相应的定义。您在这里有两个选择:要么像这样在 main 之前提供定义:

    unsigned int Student::count_total_no_of_students_enrolled = 0;

或考虑使用 C++17 的 inline variables:

    class Student {
    static inline unsigned int count_total_no_of_students_enrolled = 0;
    //...
    }

在 C++17 之前,第一个是唯一可用的选项。这是因为,class 声明并没有真正分配存储空间:它只是一个声明。但是,您的 static 变量独立于 class 的对象而存在,因此您必须在外部某处为它们提供单一定义,以便编译器可以分配存储空间。 C++17 的 inline 变量使它更符合人体工程学。