paras
paras

Reputation: 237

Using struct in different .cpp file

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.

Upvotes: 22

Views: 52007

Answers (9)

Martin Gunia
Martin Gunia

Reputation: 1141

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 linker errors. Now you have access to globalStruct from each compilation unit that includes mystruct.h.

Upvotes: 39

kravemir
kravemir

Reputation: 10986

If you want to share any variable between multiple cpp files, you should declare it in header as extern. And without extern in one of that c++ files.

If you don't do it, it'll lack at linking, because multiple objects would have variable with same name. Instead when using extern one object would have this variable and other objects link it.

Upvotes: 0

A. K.
A. K.

Reputation: 38136

May be you can give more information about what is the layout of your project.

Going by the guess, probably your problem can be either of the two:

  1. you want forward declaration of struct.

  2. using include guards to prevent redefinition.

See the following link for how to handle both:

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

The header files also use include guards, so you can figure out what exactly can solve your problem.

Upvotes: 0

unkulunkulu
unkulunkulu

Reputation: 11912

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

Upvotes: 0

user195488
user195488

Reputation:

The header is where you declare what your struct will consist of (probably a common.h file included by main.cpp and myfile.cpp):

struct MyStruct {
    int messageID;
    int tempVariable;
};

In your main.cpp, this is where you actually use the struct:

void someFunction() {
    struct MyStruct tempStruct;

    // do something with it
    tempStruct.messageID = 1;

}

Don't put the definition of your struct in both your main.h and main.cpp - or you will get a redefinition error!

Also, don't include the cpp file - include the header file (e.g. common.h). Without knowing more about the structure of your program, it is hard to provide better information.

Upvotes: -1

Ajay
Ajay

Reputation: 18411

The 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"

Upvotes: 0

Vern
Vern

Reputation: 2413

Declaration and definitions are two different things. For your case, you are allocating space for your structure in main.cpp. In your header, you should use the extern modifier for your struct so that all files that include the header file will look in the global namespace for the structure. Hope it helps.

Upvotes: 0

cprogrammer
cprogrammer

Reputation: 5675

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

Upvotes: 2

Daniel Wehner
Daniel Wehner

Reputation: 2189

The problem is that you basically have the same code twice as a result if you see an include as just a import of the code.

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

Upvotes: 0

Related Questions