首页 > 解决方案 > 试图将指向对象的指针数组传递给 C++ 中另一个类的函数(不允许使用向量)

问题描述

我有一个名为学生的班级。我在我的主要对象中创建了许多学生对象(每个对象代表一个学生。)我真正想做的是将这些学生中的每一个传递给学校班级的功能输入,代表一个学生进入学校然后打印他/她的名字等。这是代码:

我的 study.h 文件包括:

#include <iostream>
#include <cstring>

using namespace std;
  
  class Student
    {
    private:
        string name;
        int no_floor;
        int no_classroom;
    public:
        Student(const string& nam,int no_fl,int no_cla)//constructor
        : name(nam), no_floor(no_fl), no_classroom(no_cla)
        {
          cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
        };
        ~Student() //destructor
       {
           cout << "A Student to be destroyed! with name " << name << " is at floor: " << no_floor << " class: " << no_classroom;
       };
    

然后是学校课:

class School
       {
       private:
             Student* pointer_array[2];
       public:
             School()//constructor
             {
               cout << "A New School has been created!" << endl;
             };
             ~School(){//destructor
               cout << "A School to be destroyed!" << endl;
             };
             void enter(Student student, int stc=0/*student counter*/);
       };

在我的 main.cpp 文件中:(每个学生的内存分配)

#include <iostream>
#include <cstring>
#include <study.h>

using namespace std;
int main(void)
{
      //Student creation
   int i,floor,classroom;
   string stname;
   Student* students[2];
   for(i=0; i<2; i++)
   {
      cin >> stname;
      cin >> floor;
      cin >> classroom;
      students[i] = new Student(stname, floor, classroom);
   }



   School sch;
   for(i=0; i<2; i++)
   {
      sch.enter(*(students[i]),i);
   }

   for(i=0; i<2; i++)
   {
     delete students[i];
   }
}

最后在我的 study.cpp 文件中,我得到了 School 类函数,我试图通过引用传递每个对象,而不是将它们复制到一个新对象

#include <iostream>
#include <cstring>
#include <study.h>

using namespace std;

void School::enter(Student student, int stc/*student counter*/)
{
  pointer_array[stc] = &student;
  cout << "pointer array[" << stc <<  "]:" << pointer_array[stc] << endl; 
  //^ this cout prints the same adress for both students array[0]:0x1ffefffd20
  //                                                     array[1]:0x1ffefffd20

}

关于如何将指针传递给所有学生而不仅仅是一个学生的任何想法。我再次尝试通过引用传递数组。想法?

标签: c++classoopobjectpass-by-reference

解决方案


这是解决这个问题学校课程的方法:

class School
{
private:
      Student* pointer_array[5];
public:
      School()//constructor
      {
        cout << "A New School has been created!" << endl;
      };
      ~School(){//destructor
        cout << "A School to be destroyed!" << endl;
      };
      void enter(Student* student, int stc=0/*student counter*/);
};

学生班级不变:

class Student
    {
    private:
        string name;
        int no_floor;
        int no_classroom;
    public:
        Student(const string& nam,int no_fl,int no_cla)//constructor
        : name(nam), no_floor(no_fl), no_classroom(no_cla)
        {
          cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
        };
        ~Student() //destructor
       {
           cout << "A Student to be destroyed! with name " << name << " is at floor: " << no_floor << " class: " << no_classroom;
       };

然后main.cpp:

#include <iostream>
#include <cstring>
#include <study.h>

using namespace std;
int main(void)
{
      //Student creation
   int i,floor,classroom;
   string stname;
   Student* students[2];
   for(i=0; i<2; i++)
   {
      cin >> stname;
      cin >> floor;
      cin >> classroom;
      students[i] = new Student(stname, floor, classroom);
   }



   School sch;
   for(i=0; i<2; i++)
   {
      sch.enter(students[i],i);
   }

   for(i=0; i<2; i++)
   {
     delete students[i];
   }
}

最后是 study.cpp 函数:

void School::enter(Student* student, int stc/*student counter*/)
{
  pointer_array[stc] = student;
  (pointer_array[stc])->print();
  cout << " enters school!" << endl;
  cout << "pointer array[" << stc <<  "]:" << pointer_array[stc] << endl;
}

推荐阅读