• Advertisement
Sign in to follow this  

Multiple Header problem

This topic is 2679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had been splitting up a large main file into multiple smaller files when I found out that you couldn't include the same header twice anywhere. This is a problem for me because I often need to reuse code and functions all over the place and its a bit hampering to have to only use those functions in one cpp file. Often I will have to include the header in the man then later realize I need to use the functions in another file but will be unable to.

Please, does anyone have any way to work around this problem and reuse functions in different files.

Share this post


Link to post
Share on other sites
Advertisement
Include guards don't let me include a file twice, they stop me from including the same file twice. I still get the error for not knowing what the function is.

Share this post


Link to post
Share on other sites
Then you might want to post actual code giving you errors and the actual errors. There's only so much we can do when you just say stuff doesn't work.

Share this post


Link to post
Share on other sites
<source>

//header 1

class{/*important data*/};

//file 2

#include "header1"

/*function that uses that data*/

//file 3
#include "header1"

//needs to use important data.
</source>


How do I get to use the header 1 data to be used in 2 different files.

Share this post


Link to post
Share on other sites
My actually code is way to long. You don't need to know the specifics, just the basic idea. The code I posted before would give multiple included errors.

What would you do to use data from a header file in multiple cpp files?

Share this post


Link to post
Share on other sites
The code you posted doesn't have include guards and wouldn't compile because you didn't give the class a name.

Share this post


Link to post
Share on other sites
Fine. I would kill at this point to get an answer so I will humor you.


//header 1
#ifndef _HEADER1_H_
#define _HEADER1_H_
class data{/*important data*/};
#endif
//file 2

#include "header1"

/*function that uses that data*/

//file 3
#include "header1"

//needs to use important data.


hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by rnw159
What would you do to use data from a header file in multiple cpp files?
This:

// Header1.h
#ifndef HEADER1_H
#define HEADER1_H

class Class1
{
public:
Class1() {}
};

#endif


// File1.cpp
#include "Header1.h"

void foo(Class1 classCopy)
{
}


// File2.cpp
#include "Header1.h"

void bar(Class1 classCopy)
{
}


Share this post


Link to post
Share on other sites
edit: (only original post was here when i wrote this, seems like you got the help you needed already! good luck!)

are you splitting up in header and source files, or moving everything into headers only?

Code should go into source files, and declarations into headers.
This will help you avoid dependency issues, taking use of forward declarations. Note that you usually want to get more uniqe header guards (examples: by pre/postfixing with some coding rules from your code standard. (project name, path, random numbers, etc))

// in header (foo.h)
#ifndef foo_h
#define foo_h
class Bar;
void foo(const Bar& bar);
class Foo {/* members&functions*/};
#endif

// in source (foo.cpp)
#include "foo.h"
#include "bar.h"
void foo(conts Bar& bar)
{
// code
}

// in header (bar.h)
#ifndef bar_h
#define bar_h
class Foo;
void bar(const Foo& foo);
class Bar {/* members&functions*/};
#endif

// in source (bar.cpp)
#include "bar.h"
#include "foo.h"

void bar(const Foo& foo)
{
// code
}




now, if you just split that up into two header files you would be in trouble

// foo.h
#ifndef foo_h
#define foo_h
#include "bar.h"

class Foo {};
// compiler needs to know about Bar here.
void foo(const Bar& bar)
{
bar.dostuff();
}
#endif

// bar.h
#ifndef bar_h
#define bar_h
#include "foo.h"
class Bar {};
// here the compiler needs to know about Foo, but since the header guard from foo prevents us from including it, we are screwed.
void bar(const Foo& foo)
{
foo.dostuff();
}
#endif


Share this post


Link to post
Share on other sites
Thankyou for giving me a straight answer. I will try that out and see what happens.

Share this post


Link to post
Share on other sites
Quote:
Original post by rnw159
When I try that, even at a small scale on a new project, I still get multiple definition errors.
What errors?

Share this post


Link to post
Share on other sites
Quote:
Original post by rnw159
Fine. I would kill at this point to get an answer so I will humor you.

Congratulations! This code that you've posted compiles and links without errors. Now do you want to try actual code that's giving you actual errors?

Share this post


Link to post
Share on other sites
Please don't be a sarcastic prick.

To everyone else who helped me, thankyou. I believe I had a few non declarations in my header files. I think I have it just about fixed. Thanks again.

Share this post


Link to post
Share on other sites
Let me get this straight, I try to help you and you refuse to post the code that's actually giving you trouble or any error messages, and you think I'm the prick? Rate--.

Incidentally, if your problem was that you had definitions in your headers, then the problem would have shown up as a linker error, which is problem four in the article I posted, which any of us could have told you immediately if you had bothered to post error messages like you were asked multiple times.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Let me get this straight, I try to help you and you refuse to post the code that's actually giving you trouble or any error messages, and you think I'm the prick? Rate--.


Whoops, I tripped and fell over my own stupid with my face full on the "rate--" button!

Share this post


Link to post
Share on other sites
Quote:
Original post by rnw159
Please don't be a sarcastic prick.


rnw159, this is not a customer service hotline. People here are generally trying to help each other... for free. To get the help you are looking for you should cooperate to find a solution to your problem. This also applies to the "real world" (TM).

cheers

Share this post


Link to post
Share on other sites
Sorry, its late. I had been grinding my head against this problem for hours. Thanks again for all your help.



In function `ZN7Monster3runEii':
multiple definition of `Map'

For anyone interested that was more or less what the error looked like.

Thanks again.


btw: rated you all up.

Share this post


Link to post
Share on other sites
Quote:

Please don't be a sarcastic prick.

Nobody is being a sarcastic prick. At least nobody was until the last few posts (hint: stop that). What you are failing to grasp is that there are several interrelated issues that could be occurring in your actual code. Far more issues than most of us could probably reliable enumerate on our own and even then they'd be guesses which would likely send you down the wrong path. Providing the code or at least a minimal subset of the code that reproduces the problem is the best, and some times the only way to get help.

You should never assume that you know how much code is "enough" to provide, because in doing so you are assuming you know what is appropriate to strip out, which means in most cases you should already know what the problem is. Since you don't, you shouldn't make the assertions you've made.

You had not provided a sufficient robust problem description for anybody to give you accurate help, so you were queried for more information. That's how it works. If you are offended by that or feel you cannot be bothered to follow up with additional information when requested, I'm afraid you won't find much help.

Share this post


Link to post
Share on other sites
It might be a good idea to setup a separate project for this kind of thing. If you have errors like this, you can take your code into the new project, and try and reduce the code bit by bit until you have a minimal representation of the problem. This process can often result in you solving the issue yourself, because finding the line or lines that are the difference between the error appearing and disappearing can often be a vital step to understanding the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement