如何在类中声明一个结构体? [英] How do I declare a struct within a class?
本文介绍了如何在类中声明一个结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个类中声明一个结构体,该结构体是私有的,我想给同一个结构体中的变量赋予一个字符值,但是我不能初始化它或者它:
I want to declare a struct within a class which is private and I want to give a character value to a variable in the same struct, but I can't initialize it or cin it:
class puple
{
private:
struct p
{
char name[25];
int grade;
};
public:
puple(){};
void setme()
{
this->p::grade=99;
this->p::name[25]='g'; //here is the problem
}
void printme()
{
cout<<"Name: "<<this->p::name<<endl;
cout<<"Grade: "<<this->p::grade<<endl;
}
};
void main()
{
puple pu1;
pu1.setme();
pu1.printme();
}
推荐答案
称为p,它是一个结构体。还没有类型p的东西。因此您的
You've describe a type called "p" which is a struct. There is yet no thing of type p around. Therefore your
p->...
调用没有任何意义。
尝试声明
p pInstance;
并使用它,例如:
void setme()
{
this->pInstance.grade=99;
this->pInstance.name[25]='g'; //here is the problem
}
请注意, ]将失败,因为该数组的允许索引为0到24(总共25个元素)。
Note even with this your assignment to name[25] will fail as the allowed indices for that array are 0 up to 24 (totalling 25 elements).
这篇关于如何在类中声明一个结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文