Using structure in another .cpp file

I have two .cpp files in one project: main.cpp and myfile.cpp

I have globally defined struct mystruct in main.cpp, now I want to use this structure in myfile.cpp. When I write a puzzle in the header file and include cpp in both files, I get an error message telling me to override the riddles. How do I solve this problem.

+7
source share
9 answers

If you are trying to share a structure definition between several compilation units (cpp files), the general way is this: put the definition of your structure in the header file (mystruct.h). If the structure contains any methods (i.e., by default it is a class with all public members), you can implement them in the mystruct.CPP file or, if they are lightweight, directly inside the structure (which makes them inline by default).

mystruct.h:

#ifndef MYSTRUCT_H #define MYSTRUCT_H struct MyStruct { int x; void f1() { /* Implementation here. */ } void f2(); /* Implemented in mystruct.cpp */ }; #endif 

mystruct.cpp

 #include "mystruct.h" // Implementation of f2() goes here void MyStruct::f2() { ... } 

You can use your structure in as many cpp files as you like, just #include mystruct.h:

main.cpp

 #include "mystruct.h" int main() { MyStruct myStruct; myStruct.x = 1; myStruct.f2(); // etc... } 

If, on the other hand, you are trying to pass a global instance of the structure through several compilation units (this is not entirely clear from your question), do as described above, and also add

 extern MyStruct globalStruct; 

to mystruct.h. This will indicate that the instance is available with external communication; in other words, that the variable exists, but is initialized elsewhere (in your case in mystruct.cpp). Add global instance initialization to mystruct.cpp:

MyStruct globalStruct;

It is important. Without manually creating an instance of globalStruct you will get . You now have access to globalStruct from each compilation module, which includes mystruct.h.

+18
source

The problem is that you basically have the same code twice as a result, if you see that include is just importing the code.

You can use #ifdef to fix it, see http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html

0
source

You must move the general structure to the header file and include this header in both files. Any other solution is a workaround.

0
source

Declaration and definitions are two different things. In your case, you allocate space for your structure in main.cpp. In your header, you must use the extern modifier for your structure so that all files containing the header file look into the global namespace for the structure. Hope this helps.

0
source

Standard C / C ++ approach:

 // source.h Put all struct, class, typedef, macro definitions, extern variable declaraltions // source.cpp Implement the class, functions, define global/external variables // main.cpp, and other parts of program #include"source.h" 
0
source

The header says that your struct will consist of (possibly a common.h file included by main.cpp and myfile.cpp ):

 struct MyStruct { int messageID; int tempVariable; }; 

In your main.cpp here, you are actually using a struct:

 void someFunction() { struct MyStruct tempStruct; // do something with it tempStruct.messageID = 1; } 

Do not put your struct definition in both main.h and main.cpp - or you will get an override error!

Also, do not include the cpp file - specify a header file (for example, common.h ). Without knowing more about the structure of your program, it is difficult to provide better information.

0
source

You must define the structure in the header file only , you must remove the definition from main.cpp

0
source

Maybe you can give more information about what your project layout is.

Assuming perhaps your problem can be one of two things:

  • you want to pass the declaration of the structure.

  • using locks to prevent overrides.

See the following link: how to work with both:

http://www.adp-gmbh.ch/cpp/forward_decl.html

Header files also include include attributes, so you can figure out what exactly might solve your problem.

0
source

If you want to share any variable between several cpp files, you must declare it in the header as extern . And without extern in one of these C ++ files.

If you do not, it will not have references, because several objects will have a variable with the same name. Instead, when using extern one object will have this variable, and other objects will communicate with it.

0
source

All Articles