Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

Iinclude guards not working?

This topic is 3171 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

Whenever I try to include this in more then one .cpp file, I always get a multiple definition error. I thought include guards were supposed to prevent that? What am I doing wrong? Also, the namespace isn't necessary. Pretty much anything I put in there will cause the same error.
#ifndef COLLISIONDETECTION_H
#define COLLISIONDETECTION_H

#include <cassert>

namespace CollisionDetect
{

    bool RectRect(double left1, double top1, double right1, double bot1, double left2, double top2, double right2, double bot2)
    {
        assert(right1>=left1);
        assert(bot1>=top1);
        assert(right2>=left2);
        assert(bot2>=top2);

        //Performs a simple collision check between two rectangles using the seperating axis theorem
        if (left1>right2) {return false;}
        if (left2>right1) {return false;}
        if (top1>bot2) {return false;}
        if (top2>bot1) {return false;}

        return true;
    }

}

#endif

Share this post


Link to post
Share on other sites
Advertisement
Post the actual error you're getting because I suspect it's a linker error, not a compiler error. If a function is defined in a header it needs the inline keyword. If it's not really appropriate for inlining it should be in a .cpp file, not a header

-me

Share this post


Link to post
Share on other sites
Quote:

I always get a multiple definition error. I thought include guards were supposed to prevent that?

That's a link error; include guards only prevent multiple passes over the header during the preprocessor phase (and thus only prevent multiple definitions within the same translation unit). Since each translation unit is compiled in isolation, however, you'll get a definition per-TU and thus a multiple-definition error when the linker tries to assemble the final product.

Kylotan's article has more details.

Share this post


Link to post
Share on other sites
Quote:
Original post by Storyyeller
Whenever I try to include this in more then one .cpp file, I always get a multiple definition error.

Do you define global variables in the header? That doesn't work. You need to declare them with the extern keyword, and have a definition in one cpp file only.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!