accessing member of structure within a class

28,068

Solution 1

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

Solution 2

In your class Foo, you have defined a packet struct, but you have not declared any instances of it. What you want is (this is a compileable self-contained example):

#include <iostream>

class Foo {
public:
  struct Packet{
    Packet() : x(0), y(0) {}
    int x;
    int y;
  } packet;
};

int main(int, char**)
{
  Foo foo_1;
  std::cout << "The value of x is: " << foo_1.packet.x << std::endl;
  std::cout << "The value of y is: " << foo_1.packet.y << std::endl;
}

Solution 3

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
}
Share:
28,068
Quick Silver
Author by

Quick Silver

Grad Student!

Updated on July 09, 2022

Comments

  • Quick Silver
    Quick Silver almost 2 years

    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.

  • Quick Silver
    Quick Silver almost 11 years
    Throws me error: code_1.cpp:117: error: no matching function for call to ‘foo::foo()’ code.hpp:170: note: candidates are: foo::foo(const char*) code.hpp:159: note: foo::foo(const foo&) make: *** [code_1] Error 1
  • Mark Garcia
    Mark Garcia almost 11 years
    @learning That's a problem with your constructor and, with due respect, is out of the scope of your question. Sorry.
  • Quick Silver
    Quick Silver almost 11 years
    Not a problem. Thanks for the help. Below comment says its a bad idea to declaring a pointer and point it to the address of a class. Will it be a problem, assigining foo_1, address of foo.
  • Mark Garcia
    Mark Garcia almost 11 years
    @learning You'll face many problems with pointers. My advice? Minimize their use. Just do foo myFoo; and you'll never have to deal with the intricacies of pointers.
  • Quick Silver
    Quick Silver almost 11 years
    Due to constructr signature, I receive error as: error: no matching function for call to ‘foo::foo()’ code.hpp:170: note: candidates are: foo::foo(const char*) code.hpp:159: note: foo::foo(const foo&) Any idea how I can resolve this error?
  • David G
    David G almost 11 years
    @learning Sorry about that. You need to add a default constructor to your class: foo() {}. I'll update my post with the code.
  • Quick Silver
    Quick Silver almost 11 years
    Got it. Thanks for the help.
  • Kuba hasn't forgotten Monica
    Kuba hasn't forgotten Monica almost 11 years
    @Mark Garcia: That is until he tries to return a pointer to a temporary variable that has just happened to cease to exist as the function returns :/