【问题标题】:private static variable accesing output is linker issues [duplicate]私有静态变量访问输出是链接器问题[重复]
【发布时间】:2020-04-11 14:34:12
【问题描述】:

我用实例数据成员创建了一个学生班级 像学生姓名、学生地址、学生姓名 我声明了可以在所有类实例之间共享的私有静态数据成员 CollegeName。 使用 promptfunction 从控制台获取用户输入 显示学生详细信息的下一个功能。 同时导致以下错误:

同时代码放在这里:

  #include <iostream> 
  #include <string>

  using namespace std;


    class Student {

     private:
      int studentrollno;
      string studentName;
      string studentAddress;
      static string CollegeName;

     public:
        void setRollNo(int rollno) {
         studentrollno = rollno;
       }
       int  getRollNo() {
         return studentrollno;
       }

       void setName(string name) {
        studentName = name;
       }
      string getName() {
        return studentName;
       }

      void setAddress(string address) {
        studentAddress = address;
       }

      string getAddress() {
        return studentAddress;
      }

      static void setCollegeName(string collegename) {
        CollegeName = collegename;
       }

      static string getCollegeName() {
        return CollegeName;
      }


      void displayStudentDetails(); // member functions declare inside class Meanwhile it is defined 
      outside

     static void show() {
         cout << "this is static function " << endl;
     }
    };



      // member functions define outside Student class
       void Student :: displayStudentDetails() {
        cout << "Student Name : " << getName() << " Student RollNo :  " << getRollNo() << "Student Address 
      :  " << getAddress() << "CollegeName: "<< getCollegeName() << endl;
     }


    void promptValues(Student &student) {


      cout << "Enter the student Details " << endl;

      cout << "Enter the details about student objects " << endl;
      cout << endl;


       int studentrollno;
       string  studentname ,  studentaddress ;
      string collegename;
       cout << "Enter the RollNo of the student  " << endl;

       cin >> studentrollno;

     cout << "Enter the Name of the student  " << endl;
     cin >> studentname;
     cout << endl;

     cout << "Enter the address of the student  " << endl;
     cin >> studentaddress;
     cout << endl;

      cout << "Enter the collegeName of the student " << endl;
      cin >> collegename;
      student.setRollNo(studentrollno), student.setName(studentname), student.setAddress(studentaddress), 
      student.setCollegeName(collegename);

    }

    int main() {


      Student student1, student2 , student3 , student4 , student5 ;
      Student studentarrays[5] = { student1, student2, student3, student4, student5 };
      Student studentmodel[5];

       for (int i = 0; i < 5; i++) {
        Student model;
        model   = studentarrays[i];
         promptValues(model);
       studentmodel[i] = model;

      }

      for (Student studentdetails : studentmodel) {
        studentdetails.displayStudentDetails();
      }


      Student::show();


     return 0;
    }

【问题讨论】:

    标签: c++


    【解决方案1】:

    您省略了静态成员Student::CollegeName 的定义。它只在类中声明,现在你应该在类声明之后定义它:

    std::string Student::CollegeName;
    

    更多信息见https://en.cppreference.com/w/cpp/language/static

    【讨论】:

      猜你喜欢
      • 2018-05-15
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多