【问题标题】:Trouble initializing and incrementing a static member variable初始化和递增静态成员变量时遇到问题
【发布时间】: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。非常感谢您提供的任何帮助

【问题讨论】:

    标签: c++ variables static


    【解决方案1】:

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

    1. 您对display_total_no_of_student() 的调用没有为该函数提供类对象或类名,因此编译器正在寻找该名称/签名的“免费”函数,但它没有找到。

    2. 虽然您已经为静态count_total_no_of_students_enrolled 成员提供了声明,但您还没有给出它的实际定义(必须在类定义之外完成)。

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

    这是您的代码的“工作”版本,解决了这些问题:

    #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;
    }
    

    请随时要求任何进一步的澄清和/或解释。

    【讨论】:

    • 该死!谢啦。你让我明白了:)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      扩展 Adrian 关于static 的回答:

      您只需在类中声明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 变量独立于您的类的对象存在,因此您必须在外部某处为它们提供单个定义,以便编译器可以分配存储空间。 C++17 的 inline 变量使它更符合人体工程学。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2020-12-09
        • 1970-01-01
        相关资源
        最近更新 更多