21

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.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
paras
  • 227
  • 1
  • 2
  • 3

9 Answers9

36

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.

Zak The Hat
  • 317
  • 1
  • 5
  • 15
Martin Gunia
  • 1,091
  • 8
  • 12
1

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

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
0

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

Daniel Wehner
  • 2,159
  • 1
  • 18
  • 22
0

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.

Vern
  • 2,393
  • 1
  • 15
  • 18
0

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"
Ajay
  • 18,086
  • 12
  • 59
  • 105
0

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

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
  • extern is not necessary if you have a common header file. Extern is only necessary if you want to have a global struct object shared by others. –  Jul 30 '11 at 13:15
  • @0A0D, I understood Op's question exactly this way. Pay attention to redefinition linkage error mentioned in the question. – unkulunkulu Jul 30 '11 at 13:18
  • He is redefining MyStruct twice.. extern won't fix it. Extern means external linkage. The compiler will trust the programmer later on that the object will be located by the linker. Otherwise, it will throw a linker error. He does not have a linker error. You should learn how extern works. –  Jul 30 '11 at 13:34
  • @0A0D, I know how it works, I just understood the question wrongly, ok, there's structure redefinition, not variable. If you look at my code, you'll see that I'm not trying to apply `extern` to a struct definition. – unkulunkulu Jul 30 '11 at 13:36
  • No, you were applying extern to a struct *declaration*. Either way, it was wrong. –  Jul 30 '11 at 13:41
  • @0A0D, No, take it look at it again, I was applying it to a variable definition, can't you differentiate between declaring a struct and declaring a variable with of this struct type? It was C-style variable declaration, so `struct` was repeated there, but that doesn't define a struct. Don't teach me. – unkulunkulu Jul 30 '11 at 13:43
  • You obviously don't understand what I am saying. `extern struct someStruct` is a DECLARATION not DEFINITION. That `extern` declaration tells the compiler that the struct DEFINITION is located elsewhere, usually in some other file. If the keyword `extern` was removed, then it would be an automatic object and memory would be allocated for that `struct` IFF the `struct` had already been DEFINED in a header file! –  Jul 30 '11 at 13:55
  • @0A0D, but my code was about `extern struct someStruct SOMEVARIABLE`. – unkulunkulu Jul 30 '11 at 13:57
  • @0A0D let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1979/discussion-between-unkulunkulu-and-0a0d) – unkulunkulu Jul 30 '11 at 13:58
  • Let's not continue in chat. This is done. Go pick up a C/C++ book or go here: http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.4.html –  Jul 30 '11 at 13:58
0

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.

A. K.
  • 34,395
  • 15
  • 52
  • 89
0

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.

kravemir
  • 10,636
  • 17
  • 64
  • 111
-1

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.

  • Why would you ever make a `main.h` file? Also, a `.h` file typically where you **define** classes/structures and **declare** methods/functions. – Elliott Mar 14 '20 at 05:42