在另一个类中使用一个类的对象
Using an object of a class in another class
我有一个名为Student的类,它看起来像这个
class Student
{ string name;
unsigned long int ID ;
string email;
unsigned short int year;
public :
Student() // Constructor
string getName(void);
unsigned long int getID(void);
string getEmail(void);
unsigned short int getYear(void);
{
以及另一个名为eClass 的类
class eClass
{
string eclass_name;
Student* students[100];
unsigned int student_count;
public:
eClass(string name)
{
student_count=0 ;
eclass_name = name ;
}
void add(Student& obj)
{
bool res = exists(Student obj); ****
if (res)
{
}
else
{
students[student_count] = obj ; ****
student_count++ ;
}
}
bool exists(Student &obj)
{
unsigned long int code = obj.getID(); ****
bool flag = FALSE ;
for (int i = 0 ; i<=student_count ; i++ )
{
unsigned long int st = students[i]->getID();
if (code==st)
{
flag = TRUE;
}
}
return flag;
}
};
它基本上创建了一个表示课程的对象,然后在检查学生是否已经属于该课程后,通过add()将学生添加到该课程中。
我用***标记的行出现错误。有人能帮我出什么问题吗。。。我不太确定我是否理解如何在另一个类中使用类的对象。
这是不正确的:
bool res = exists(Student obj); // ****
它应该是这样的:
bool res = exists(obj); // ****
obj
是可在函数内部使用的函数(类型为Student
)的参数。在这一行中,您正在使用该参数将其传递给另一个函数。
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 包装一个对象并假装它是一个 int
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 如何使用函数将一个对象的输入复制到另一个对象中
- 选择一个元素而不是一个对象的数组的原因
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 打印对象的映射,其中另一个对象作为键
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 绘制一个对象,比较模具缓冲区的两个不同值