试图将指向对象的指针数组传递给 C++ 中另一个 class 的函数(不允许使用向量)
Trying to pass an array of pointers to objects, to a function of another class in C++ (not allowed to use vectors)
我有一个叫 class 的学生。我在我的主体中创建了许多学生对象(每个对象代表一个学生。)我真正想做的是将这些学生中的每一个传递给学校 class 的功能,代表一个学生进入学校并且然后打印 his/her 名称 etc.Here 的代码:
我的 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:
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 class 函数,我试图通过引用 传递每个对象,而不是将它们复制到新的对象:
#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
}
关于如何将指针传递给所有学生而不仅仅是一个学生的任何想法。我再次尝试通过 reference.Thoughts?
传递数组
这是解决这个问题的方法
学校class:
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没有变化:
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;
}
我有一个叫 class 的学生。我在我的主体中创建了许多学生对象(每个对象代表一个学生。)我真正想做的是将这些学生中的每一个传递给学校 class 的功能,代表一个学生进入学校并且然后打印 his/her 名称 etc.Here 的代码:
我的 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:
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 class 函数,我试图通过引用 传递每个对象,而不是将它们复制到新的对象:
#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
}
关于如何将指针传递给所有学生而不仅仅是一个学生的任何想法。我再次尝试通过 reference.Thoughts?
传递数组这是解决这个问题的方法 学校class:
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没有变化:
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;
}