简体   繁体   中英

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.

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 (ie 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.

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

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

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.

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 (eg common.h ). Without knowing more about the structure of your program, it is hard to provide better information.

您应该在头文件中定义结构,您应该从main.cpp删除定义

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.

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM