Jump to content

Newbie, trying to use custom objects in my custom library

- - - - -

  • Please log in to reply
11 replies to this topic

#1
Quillion

Quillion

    Newbie

  • Members
  • Pip
  • 7 posts
Thank you very much in advance to anyone who will help. I have gotten a liking to making games and I see the horrible downfalls in speed with java because of graphics and am looking into C++ and openGL. So I decided to import my engine from java into C++, but it won't work properly. Here is what I do. This is my object class, a simple bounding box, I do not post my cpp because there is really no need for it I assume header should suffice right?

class BoundingBox

{

public:

	BoundingBox();

	~BoundingBox();


	void setX(float value);

	float getX();

	void incrementX(float value);


	void setY(float value);

	float getY();

	void incrementY(float value);


	void setWidth(int value);

	int getWidth();

	void incrementWidth(int value);


	void setHeight(int value);

	int getHeight();

	void incrementHeight(int value);


	float getLeftX();

	float getRightX();

	float getTopY();

	float getBottomY();


	float getTextureLeftX();

	float getTextureRightX();

	float getTextureTopY();

	float getTextureBottomY();


	void setOffsets(float left, float right, float top, float bottom);


	void setTopOffset(float top);

	float getTopOffset();

	void setBottomOffset(float bottom);

	float getBottomOffset();

	void setLeftOffset(float left);

	float getLeftOffset();

	void setRightOffset(float right);

	float getRightOffset();


private:

	float x, y, leftOffset, rightOffset, topOffset, bottomOffset;

	int width, height;

};

Now I have created an engine library which checks for collisions, but I can't use my BoundingBox object no matter what I do, this is what my Engine header and class look like:

My header

#include "BoundingBox.h"


extern bool Collision(BoundingBox *box1, BoundingBox *box2);

extern bool Collision(BoundingBox &box1, BoundingBox &box2);

My cpp file

#include "Engine.h"

#include "BoundingBox.h"


bool Collision(BoundingBox *box1, BoundingBox *box2)

{

	if(box1->getLeftX() <= box2->getRightX() && box1->getRightX() >= box2->getLeftX())

	{

		if(box1->getTopY() <= box2->getBottomY() && box1->getBottomY() >= box2->getTopY())

		{

			return true;

		}

	}


	return false;

}


bool Collision(BoundingBox &box1, BoundingBox &box2)

{

	if(box1.getLeftX() <= box2.getRightX() && box1.getRightX() >= box2.getLeftX())

	{

		if(box1.getTopY() <= box2.getBottomY() && box1.getBottomY() >= box2.getTopY())

		{

			return true;

		}

	}


	return false;

}

I have tried searching everywhere, but I can not find the reason why this issue occurs. I am using Visual C++ 2010 express if it is of any help. Thank you very much again for all your help in advance, I am quiet lost and been stuck on this for two days now.

#2
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
Can you post what your problem is? you just said you can't use BoundingBox....

but why?
are you getting a compiler error?
runtime error?

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
As Skippy said, we can't tell without saying what compiler/runtime errors do you get. Also, I don't see why do you have pretty much the same function twice (Collision).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
Quillion

Quillion

    Newbie

  • Members
  • Pip
  • 7 posts
Ok so first answer is on me using collision function twice. I have absolutely no idea how pointers work, so if I want to check for the collision I just created two methods, one uses the * thing and the other the &, is there a difference? Sorry I originally made it in java so I am really bad at C/C++.
And now the second answer. I get a compile error, and this is what it says

1>------ Build started: Project: Game, Configuration: Release Win32 ------

1>  Game.cpp

1>C:\Users\Quillion\Documents\Visual Studio 2010\Projects\Game\inc\BoundingBox.h(2): error C2011: 'BoundingBox' : 'class' type redefinition

1>          C:\Users\Quillion\Documents\Visual Studio 2010\Projects\Game\inc\BoundingBox.h(2) : see declaration of 'BoundingBox'

1>..\src\Game.cpp(19): error C2027: use of undefined type 'BoundingBox'

1>          C:\Users\Quillion\Documents\Visual Studio 2010\Projects\Game\inc\BoundingBox.h(2) : see declaration of 'BoundingBox'

1>..\src\Game.cpp(19): fatal error C1903: unable to recover from previous error(s); stopping compilation

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thanks for all the interest in helping me, and thank you for just helping me, this question has been a thorn on my side for a while now.

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
From a quick look at your errors, this is your error (having two Collision functions). Use one with reference ( & ).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
Quillion

Quillion

    Newbie

  • Members
  • Pip
  • 7 posts
Ok so I have removed the one with the stars and now I have only one collision function(the one with &), but I still get the same error while compiling, also I do not call that function anywhere yet. I am just trying to compile but it won't do it.

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Error C2011, check this out. I think problem lies somewhere in your headers.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
Quillion

Quillion

    Newbie

  • Members
  • Pip
  • 7 posts
Thank you for helping me so much Flying Dutchman, I read the problem on the msdn, but being new did not manage to understand anything, I tried switching the order in which I include headers in my Engine.cpp file, but that did not help. Should I install another version of Visual C++? Or should I go about doing it some other way?

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I'd ask you to post your full code if you can, you can cut out implementation in BoundingBox.cpp, but leave everything else.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
lol

try using header guards in each of your headers.

in c++ when you say to "include" something... you literally paste that header into the spot where it says #include.

Sometimes if you are not careful you will have the same header file included into the same file twice on accident.

This will result in that "class redefinition error" you are getting.

here is how you use header guards:

#ifndef _HEADER_NAME_H_

#define _HEADER_NAME_H_


// everything in header goes here


#endif

_HEADER_NAME_H_ can be whatever you want as long as it is unique.
I usually do a breakdown of where the class is located scope wise...
so
_NAMESPACE1_NAMESPACE2_CLASSNAME_H_

this would be equivalent to each of the namespaces being a package in Java.. and namespace 2 is a package that lies within namespace 1. and the class you created the file for is the CLASSNAME part.

Microsoft also has a rather unique way of doing this, but I'm warning you now, it will not work on any other compiler.

put #pragma once at the very top of each header.

That is effectively the same thing. But only works with microsoft compilers.

#11
Quillion

Quillion

    Newbie

  • Members
  • Pip
  • 7 posts
Holy crap, thank you so much Skippy, this worked like magic! Thanks a lot I really appreciate your help, now I know how to use head guards, they are awesome! Thank you very much too flying dutchman, you guys rock!.

#12
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Now that include guards are in place you can make all your headers self-contained. Example:
Files exist: Foo.h, Bar.h, Common.h - you include Common.h in both Foo.h and Bar.h - that way you can use either class Foo or Bar separately without including the other.
Imagine if you would only include Common.h in Foo.h, because you always write your main.cpp like this:
#include "Foo.h"

#include "Bar.h"

//...
since Bar relies on Common.h to be included - it will work, because it is indeed included - but in file Foo.h. Now if you want to use only Bar.h... Boom! No Common.h and lots of compile time errors. :)

Be aware, this including deal is pretty simple most of the time, but it's a bit archaic, and not what you could expect from a language that is sometimes said to be high level.

Also, GCC (gcc/g++) has a flag -E that will show you how your code looks like after preprocessor runs over it, just comment out standard headers and you'll see how #include works. ;)
g++ -ansi -pedantic -Wall -Wextra -E main.cpp > preprocessed.cpp

//open the preprocessed.cpp to see the contents - if you didn't comment out standard headers your file can be pretty big indeed





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users