Hi,

I’ve 3 classes : A, B and C. The class A includes an object from the class B and another object from the class C.

In my class B, I’ve a function that returns a vector of struct. This function is called in the class A and after that passed as parameter to a function in the class C. My problem and my question is where can I put my struct so that it can be accessible to the 3 classes.

Thank you in advance!

Put it in a header:
structHeader.h

#ifndef STRUCT_HEADER  //begin header guard
#define STRUCT_HEADER

struct someStruc {
  /* struct elements */
};

#endif //STRUCT_HEADER //end header guard

Then #include the header in your other files

classA.h

#include structHeader.h

class A {
 /* class members */
};

classB.h

#include structHeader.h

class B {
 /* class members */
};

classC.h

#include structHeader.h

class C {
 /* class members */
};

main.cpp

#include "structHeader.h"
#include "classA.h"
#include "classB.h"
#include "classC.h"

/* whatever else */

Make sure you use header guards. See the code in structHeader.h to see what I mean. Be sure you have them in all your other headers as well. If you don't, you'll get compile errors concerning redefinitions of class and/or struct names.

Declare the struct before any of the classes.

Thank you Fbody.

Ancient Dragon: How can I declare the struct before any of the classes?

Put the declaration above them, class declarations work just like function prototypes. You can't use them until after they are declared.

error:

class A { //definition of A
  struct myStruct structInstance; //will cause compile error
};

struct myStruct {  //definition of myStruct
};

okay:

struct myStruct {  //definition of myStruct
};

class A { //definition of A
  struct myStruct structInstance; //now okay because of previous definition
};

also okay:

struct myStruct; //forward declaration of myStruct

class A { //definition of A
  struct myStruct structInstance; //now okay because of forward declaration
};

struct myStruct {  //definition of myStruct
};
struct x
{
  // blabla
};

class A
{
   // blabla
};
etc.

Guys, I got an error with the following code:

struct myStruct {
           int myVar1;
      };

      class A { //definition of A
         ...
      };
struct myStruct {
           int myVar1;
      };

      class B { //definition of B
         ...
      };
struct myStruct {
           int myVar1;
      };

      class C { //definition of C
         ...
      };

Use your head for something other than a hat rack.


declare the structure only one time, not three times. If you declare the three classes in their own *.h files then put the structure in its own *.h file and include it in the header files that use it.

Guys, I got an error with the following code:

struct myStruct {
           int myVar1;
      };

      class A { //definition of A
         ...
      };
struct myStruct {
           int myVar1;
      };

      class B { //definition of B
         ...
      };
struct myStruct {
           int myVar1;
      };

      class C { //definition of C
         ...
      };

You are declaring myStruct multiple times in seperate files. You need to put myStruct in a header (explained earlier) or format the code together

struct myStruct{
     //foo
};

Class A {
     //foo
};

Class B {
     //foo
};

Class C {
     //foo
};

Are you using myStruct from main or only in the classes? It's been awhile since I actually coded, but I think you can declare the struct as private and keep the classes seperate in different files as well. I'm not 100% certain of the syntax for that though.

I think I've to put my struct in a separate header file and finish with this problem.

I'm not an expert with c++. I've thought that a class means a separate file and a struct declared inside a file is recognized internally only.

Thank you!

Its not absolutely required that each class is coded in its own file -- usually the coding standards you follow will dictate that. For example if that's the way your teacher wants it then you much follow that rule. When you start real programming you will find out that related classes are often placed in the same heder file.

If you define a structure inside a class then that structure is really only useful in the context of the structure

class A
{
public:
   struct x
   {
      // blabla
   }
};

yes declare your structure only one time in a file not in all the files where you have declare your classes. that why it is giving compile time error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.