PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

"Including GapiDrawApplication.h more than once" #2

I've got difficultys with subj.
There are topic with description same problem here (http://forums.pocketmatrix.com/viewtopic.php?t=6661).
But there is no solution.

This feature is very important for me.
Can i hope this problem will be solved in some of next versions?
I was the one who started the other thread.

My solution was pretty simple. Take all the code out of the header file (GapiDrawApplication.h) and create a new C++ file (GapiDrawApplication.cpp).

Johan did mention that he might do this in future relases.. but we have to wait and see what happens when he gets all the flooding away (good luck mate).

Hope this helps,

Sam Nova
Thank you.

PS Dunno why i didn't get it from that old post... 8)
At the top of all .h files that are to be included multiple times, add the following code:
Code:

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
This is the "proper" way of doing that.

Code:

#ifndef SOME_UNIQUE_HEADER_NAME
#define SOME_UNIQUE_HEADER_NAME
All Header Code
#endif
IceBoxman & Hosed, your suggestions will not help Mr. Prayer at all.

And as you might have seen Johan did include BOTH in GapiDrawApplication.h

But if you try to include the header file from two DIFFERENT .cpp files you will get problems.

Imagine following simple header file, lets call it a.h

Code:

#ifndef __A_H__
#define __A_H__

class test
{
public:
int var;

void setIt(int v);
};

void test::setIt(int v)
{
var = v;
}

#endif //__A_H__

Then you can have the main .cpp file as:
Code:

#include "a.h"

int main(int argc, char* argv[])
{
return 0;
}

And another .cpp file as:
Code:

#include "a.h"

This will result in the error:
extra.obj : error LNK2005: "public: void __thiscall test::setIt(int)" (?setIt@test@@QAEXH@Z) already defined in test.obj

This is probably more what Mr.Prayer is talking about. Only solution is to split code and header into two different files.. Or am I wrong ?

Cheers,
Sam
Actually the whole point of #ifndefs and #pragma once is to alleviate those problems. You wouldn't get that error from your sample code.

The best way around this is just to include the header in another header (like stdafx) and then in that header be sure to wrap the includes with #ifndef or #pragma once. Then all the sub modules that need that header should just include the main header it's in.

I do this a lot in my projects by setting up large component headers that contain all the various classes and library headers for that major component that might be needed by the individual pieces.
I do it much more simple.. Never have code (unless it is inline) in header files :) does the trick everytime.