Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using struct in different .cpp file

Tags:

c++

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

I have globaly defined struct mystruct in main.cpp, now I want to use this struct in myfile.cpp. When I write mystruct in a header file and include in both cpp files I get an error, saying mystruct redefinition. How should I solve this problem.

like image 847
paras Avatar asked Jul 30 '11 10:07

paras


People also ask

Should structs go in header files?

If a struct is declared in a header file in C++, you must include the header file everywhere a struct is used and where struct member functions are defined. The C++ compiler will give an error message if you try to call a regular function, or call or define a member function, without declaring it first.

Can we use struct in CPP?

C++ allows structs to be like classes, but one shouldn't use structs like classes.

How do I include a CPP file in another file?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.

Can you define a struct in a header file?

For a structure definition that is to be used across more than one source file, you should definitely put it in a header file. Then include that header file in any source file that needs the structure.


2 Answers

If you are trying to share the definition of a struct among several compilation units (cpp files), the common way is this: Place the definition of your struct in a header file (mystruct.h). If the struct contains any methods (i.e. it is rather a class with all member public by default), you can implement them in mystruct.CPP file, or, if they're lightweight, directly within the struct (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 struct in as many cpp files as you like, simply #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 share a global instance of the struct across several compilation units (it's not absolutely clear from your question), do as above but also add

extern MyStruct globalStruct;

to mystruct.h. This will announce that an instance is available with external linkage; in other words that a variable exists but is initialized elsewhere (in your case in mystruct.cpp). Add the initialization of the global instance to mystruct.cpp:

MyStruct globalStruct;

This is important. Without manually creating an instance of globalStruct, you'd get unresolved-external linker errors. Now you have access to globalStruct from each compilation unit that includes mystruct.h.

like image 87
Martin Gunia Avatar answered Sep 18 '22 12:09

Martin Gunia


You should move the common struct to a header file and include that header in both files. Any other solution is a workaround.

like image 28
cprogrammer Avatar answered Sep 20 '22 12:09

cprogrammer