accessing member of structure within a class

2019-05-03 07:40发布

I have an .hpp and .cpp file. I want to access the variable in the structure within a class which happens to be in the header .hpp file, in the .cpp file.

In .hpp, I have

class foo{

public:
       struct packet{
         int x;
         u_int y;
      };

};

 foo(const char*name)
:m_name(name){}

In .cpp I did:

foo *foo_1 = &foo;
printf("The value of x is : %d",foo_1->packet.x);
printf ("The value of y is : %u", foo_1->packet.y);

On doing this I receive the following error:

code_1.cpp:117: error: expected primary-expression before ‘;’ token
code_1.cpp:118: error: invalid use of ‘struct foo::packet’
code_1.cpp:119: error: invalid use of ‘struct foo::packet’
make: *** [code_1] Error 1

My objective is to get the values of x and y in the cpp file. Any suggestion/idea will be really appreciated.

Thanks.

10条回答
在下西门庆
2楼-- · 2019-05-03 08:06

The struct declaration in the class does not create an instance of it - just defines it as a contained struct within foo.
If you create a struct instance in the class, you can reference it from the containing pointer:

   struct packet{
     int x;
     u_int y;
  } m_packet;

Now you can say foo_1->m_packet.x = 3;, etc.

Also, you need to create an instance of foo (in your code you try to take the address of the class name, which won't work): foo* foo_1 = new foo;

Then, delete foo_1 when done with it.

查看更多
Bombasti
3楼-- · 2019-05-03 08:10

You need a member object of type foo::packet in class foo.

class foo{

public:
      struct packet{
         int x;
         u_int y;
      };

      packet my_packet;   // <- THIS
};

In your .cpp, you should do:

foo *foo_1 = &foo;
printf("The value of x is : %d",foo_1->my_packet.x);
printf ("The value of y is : %u", foo_1->my_packet.y);

You must remember that even though packet is inside foo, it is not included in foo as a member object. It is just a class enclosed inside another class. And for a class to be used, you must have objects of it (a class can also be used without having objects of it, but, well...).

查看更多
姐就是有狂的资本
4楼-- · 2019-05-03 08:10

Class foo does not have a member packet but it contains another class/struct type named "packet". You'll have to provide a member of that type.

Can't do a test atm but you could either try

class foo{

public:
       struct {
         int x;
         u_int y;
      } packet;

}

or

class foo{
public:
      pack packet;
      struct pack {
         int x;
         u_int y;
      };

}

To access x and y via foo_ptr->packet.(...) or foo_object.packet.(...).

查看更多
Lonely孤独者°
5楼-- · 2019-05-03 08:12

Try

struct Vehicle
    {
    int wheels;
    char vname[20];
    char color[10];
}v1 = {4,"Nano","Red"};

int main()
{
printf("Vehicle No of Wheels : %d",v1.wheels);
printf("Vehicle Name           : %s",v1.vname);
printf("Vehicle Color          : %s",v1.color);
return(0);
}

hope this may help you we gave the structure name as V1 and acessing the elements by the use of dot operator

查看更多
狗以群分
6楼-- · 2019-05-03 08:21

packet is not a data member of the class, but the class that it defines it is however. You need to instantiate an object of that type in order to use it in that way:

class foo
{
public:
       foo() {} // A default constructor is also needed
       struct
       {
         int x;
         u_int y;
      } packet;
}; // -- don't forget that semicolon

int main()
{
    foo *foo_1 = new foo(); // instantiate a new object on the heap
    printf("The value of x is : %d",foo_1->packet.x);
    printf("The value of y is : %u", foo_1->packet.y);

    delete foo_1; // release the memory
}
查看更多
何必那么认真
7楼-- · 2019-05-03 08:22

and besides tha packet-related business, foo *foo_1 = &foo; is bad, you can't take address of a class only of a variable.

查看更多
登录 后发表回答