C++ 如何在类中声明结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1609372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:36:14  来源:igfitidea点击:

How do I declare a struct within a class?

c++

提问by haddad

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:

我想在一个私有的类中声明一个结构,我想给同一个结构中的一个变量一个字符值,但我不能初始化它或cin它:

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();
}

回答by Doug T.

You've describe a type called "p" which is a struct. There is yet no thing of type p around. Therefore your

您已经描述了一种名为“p”的类型,它是一个结构体。周围还没有 p 类型的东西。因此你的

p->...

calls make no sense.

打电话没有意义。

Try declaring

尝试声明

p pInstance;

in your class and using it, ie:

在你的课堂上并使用它,即:

void setme()
{
    this->pInstance.grade=99;
    this->pInstance.name[25]='g';  //here is the problem
}

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).

请注意,即使这样,您对 name[25] 的分配也会失败,因为该数组的允许索引为 0 到 24(总共 25 个元素)。

回答by jkerian

You have two serious problems here

你这里有两个严重的问题

struct p
{
char name[25];
int grade;
};

This defines a struct type, named p. I think what you wanted to do was

这定义了一个名为 p的结构体类型。我想你想做的是

struct
{
char name[25];
int grade;
} p;

This will declare a struct, named p, with the name and grade member variables.

这将声明一个名为 p的struct,其中包含 name 和 grade 成员变量。

Your second serious problem is that you assign:

您的第二个严重问题是您分配了:

this->p::name[25]='g';  //here is the problem

This assigns 'g' to the 26th element of the array name. (arrays are 0-indexed)

这将 'g' 分配给数组名称的第 26 个元素。(数组是 0 索引的)

回答by THX-1138

isn't it

是不是

struct { ... } p; // variable of struct-type definition.

not

不是

struct p { ... }; // type 'struct p' definition.

?

?

回答by Will Bickford

Place the struct definition outside of the class using a typedef. By having the struct defined in your .cpp file it will not be visible outside of your class.

使用 typedef 将 struct 定义放在类之外。通过在您的 .cpp 文件中定义结构,它在您的类之外将不可见。

#include <iostream>
typedef struct _foo
{
    int a;
} foo;

class bar
{
public:
  void setA(int newa);
  int getA();
private:
    foo myfoo;
};

void bar::setA(int newa)
{
   myfoo.a = newa;
}

int bar::getA()
{
   return myfoo.a;
}

using namespace std;
int main()
{
  bar mybar;
  mybar.setA(17);
  cout << mybar.getA() << endl;
  return 0;
}