Need help with compile errors

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Need help with compile errors

Post by adabo » January 21st, 2017, 7:52 pm

I'm really stumped on this one, guys. There are 81 errors and I'm fairly confident that they have something to do with this:

Code: Select all

GameObject::GameObject( MainWindow &Wnd )
	:
	wnd(Wnd),
	ship(Vector( 400.f, 300.f ), 30, 30),
	enemy(Vector( 20.f, 20.f ), 30, 30),
	projectile(Vector( 0.f, 0.f), 10, 10),
	upgrade( *this ), 
	input( *this ),
	movement( *this )
{}
See the last three initializers using *this? It seems every error is related to these three objects.

The hierarchy is as follows:

Code: Select all

Game.cpp
|
|GameObject object( Mainwindow &wnd )
	|
	|Upgrade upgrade( Object &object )
	|Input input( Object &object )
	|EntityMovement movement( GameObject &object )
That's about it. I pass a reference object of class GameObject to each constructor of Upgrade, Input, EntityMovement. Just experimenting with different code orginazation.

Any help would be appreciated. Thanks!

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Need help with compile errors

Post by adabo » January 21st, 2017, 8:57 pm

Of course I forget to link to the project :oops: :roll:

https://github.com/adabo/CompileErrors

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Need help with compile errors

Post by MrGodin » January 21st, 2017, 9:34 pm

If you are going to declare a name to an object, such as Ship myShip, the class Ship must have a default constructor if you've declared a different one, which you have.
So in Ship, Input,GameObject ect .. in each of their Headers put Ship(){}, in the ship header, Input(){} in the Input header file .. and so on. Just from what i looked at.
Any reference (&) to an object or variable you delare inside you class, such as GameObject& gameObject, has to be initialized during construction of the class.
There are a lot of issues to address here. Mostly with default constructors and reference initialization within the classes.
Curiosity killed the cat, satisfaction brought him back

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Need help with compile errors

Post by adabo » January 21st, 2017, 9:56 pm

Very good, I'll start with the simplest and work my way uphill :) Thank you.

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Need help with compile errors

Post by adabo » January 21st, 2017, 10:17 pm

Pushed new changes.

Added the default constructors to all my classes. This build has 104 errors now. Digging myself a hole now lol

As for initializing the objects that have memebers that are & references, yes, it's all taken care of.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Need help with compile errors

Post by MrGodin » January 21st, 2017, 11:01 pm

Ok, I did a few thing and got it to compile
(1), moved input,movement and upgrade into Game.h
(2) initialized input with the Keyboard class
(3) initialized movement with input and cjanged movement to take an object in the Update
(4), i put some example codes in the classes described so you can see whats happening, Update movements in the Game.cpp UpdateModel function
(5) there were some interdependence of header files so i forward declared come classes in header files, such as class Input& input, then put the Input.h in the other class .cpp file (see movement.h for example)
It now compiles... hope it helps
Attachments
CompileErrors-testing_upgrade.zip
(627.32 KiB) Downloaded 132 times
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Need help with compile errors

Post by MrGodin » January 21st, 2017, 11:10 pm

In here do this

Code: Select all

void EntityMovement::Update(GameObject& obj)
{
	 MoveProjectile(obj);
	 MoveShip(obj);
	 MoveEnemy(obj);
	
}
Here

Code: Select all

void EntityMovement::MoveShip(GameObject& obj)
{
	if (input.is_space_pressed)
	{
		obj.ship.position.x += 0.1f;
	}
}
and ..

Code: Select all

void Game::UpdateModel()
{
	const float frame_time = timer.Reset();
	input.Update();
	movement.Update(object);
}
It now moves the ship when you press the space bar
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Need help with compile errors

Post by albinopapa » January 21st, 2017, 11:43 pm

You have a circular dependency in GameObject.h and Upgrade.h
You have a circular dependency in GameObject.h and EntityMovement.h
You have a circular dependency in GameObject.h and Input.h

Since you are storing references to objects, you really shouldn't have a default constructor since you don't have anything to initialize the reference with.

Instead of declaring an empty default constructor, declare the constructor with = default;

I've read that this is more efficient by letting the compiler create the default constructor, the gains might be minimal, but it does reduce the amount of typing one had to do.

So, I removed all default constructors
Forward declared class GameObject in the three files mentioned above, because they only want the game object by reference and not by value.
Moved the #include "GameObject.h" to the cpp files of the three data types mentioned above.

Now compiles.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Need help with compile errors

Post by adabo » January 22nd, 2017, 12:01 am

@MrGodin I'll take that advice. Though, admittedly I don't understand the majority of what you're explaining, I'm sure looking over the code and practicing will make it easier to make sense of it in the future.

@albinopapa Again, the circular dependencies and forward declaring. I just don't have a handle on includes. Practice, practice, practice.

Thank you both for taking the time to figure this out, I hope it wasn't too much trouble because this I'm only using this project as training aid. Wan't meant to be a full project.

Post Reply