8 Replies - 269 Views - Last Post: 29 December 2012 - 02:45 PM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Vector iterations not calling function

Posted 29 December 2012 - 12:36 PM

Here is the vector iteration loop:
for(size_t i = 0; i < gameObjects.size(); ++i)
{
	gameObjects.at(i).Draw(window);
}



gameObjects contains a Ground and a Player (both are derived from GameObject) but neither one of their Draw() functions are called.

I have this same loop later with .Update() instead of .Draw(), and that one doesnt work either.

Using the debugger is see that the line
gameObjects.at(i).Draw(window);
is run, but the program never enters the function .Draw().

Is This A Good Question/Topic? 0
  • +

Replies To: Vector iterations not calling function

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Vector iterations not calling function

Posted 29 December 2012 - 12:52 PM

You will need to show more code.

When you run your program thru your debugger what are the values of gameObjects.size(), i and window?

But without a small complete program that illustrates your problem we would only be guessing as to what is happening.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Vector iterations not calling function

Posted 29 December 2012 - 01:06 PM

Im using msvc++ 2010 express and i can't see the debugging values for some reason. I can only see breakpoints, output, and callstack.

Here is more code though with the very irrelevant parts removed.
#include <SFML/window.hpp>

#include "Player.h"
#include "Ground.h"

#include <vector>

// ** Function Declarations ** //
void Update(sf::RenderWindow& window, std::vector<GameObject>& gameObjects);
void Draw(sf::RenderWindow& window, std::vector<GameObject>& gameObjects);


// ** Program Entry Point ** //
int main()
{
	std::vector<GameObject> gameObjects;

	...
	
	Player player("Load/player.png", world);
	gameObjects.push_back(player);

	Ground ground("Load/floor.png", world);
	gameObjects.push_back(ground);


	// Create the main Window
	sf::RenderWindow mainWindow(sf::VideoMode(640,480,32), "Various Libraries Test");

	...

	while(mainwindow.isOpen())
	{
		for(int32 i = 0; i < 30; ++i)
		{
			...

			Update(mainWindow, gameObjects);
			Draw(mainWindow, gameObjects);
		}
	}

	return 0;
}


// ** Function Definitions ** //
void Update(sf::RenderWindow& window, std::vector<GameObject>& gameObjects)
{
	sf::Event Event;
	while(window.pollEvent(Event))
	{
		// close window
		if( Event.type == sf::Event::Closed)
			window.close();
	}

	for(size_t i = 0; i < gameObjects.size(); ++i)
	{
		gameObjects.at(i).Update();
	}
}

void Draw(sf::RenderWindow& window, std::vector<GameObject>& gameObjects)
{
	window.clear();

	for(size_t i = 0; i < gameObjects.size(); ++i)
	{
		gameObjects.at(i).Draw(window);
	}

	window.display();
}


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Vector iterations not calling function

Posted 29 December 2012 - 01:19 PM

You keep saying the problem is in the gameObjects.Draw() function but you haven't provided the code for this function. The Draw() function you're showing is not the same function.

Also when you ran your program thru your debugger did you place a break point in the Draw() function so you could single step into your gameObjects.Draw() function and view the variables in these functions?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Vector iterations not calling function

Posted 29 December 2012 - 01:29 PM

Yes i placed break points but i cant see the values.

And the program never gets to the gameObjects.Draw() function so i didnt think it was relevant. I still don't think its relevant but here it is anyway.

void Player::Draw(sf::RenderWindow& window)
{
	window.draw(sprite);
}


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Vector iterations not calling function

Posted 29 December 2012 - 01:39 PM

If the Draw() function is getting called but the gameObjects.draw() is not then something in your for statement is incorrect. Try printing the values to the screen or to a file.

So Player::Draw() is the same as GameObject::Draw()?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Vector iterations not calling function

Posted 29 December 2012 - 01:50 PM

The for loop seems fine. gameObjects.size() is 2(which it should be) and the value of i is 0, then 1(as it should be).


GameObject.Draw is a virtual function. Looks like this:
class GameObject
{
public:
	GameObject(){};

	virtual void Update(){;};
	virtual void Draw(sf::RenderWindow& window){;};

...


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Vector iterations not calling function

Posted 29 December 2012 - 02:06 PM

I suggest that you put a simple printout in each of the Draw functions telling you which is actually being called. For example.

virtual void Draw(sf::RenderWindow& window){std::cout << "In GameObject class" << std::endl;};


Jim

This post has been edited by jimblumberg: 29 December 2012 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Vector iterations not calling function

Posted 29 December 2012 - 02:45 PM

Does MSVC++ 2010 Express allow you to watch certain variables, like when debugging with F5 you and right click on a variable or object and add it to the watch list.

The way that you have your virtual methods is confusing. You should not have empty virtual methods, that is just asking for trouble. They should be pure virtual, and have derived classes implementing them like any other normal mechanism.

I think you need to review polymorphism.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1