3 Replies - 18502 Views - Last Post: 21 December 2012 - 02:17 AM Rate Topic: -----

#1 GryphonClaws   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Iterator Conversion problem?

Posted 20 December 2012 - 10:58 PM

I'm not sure what is the problem, as I don't see any syntax errors, and I have no idea what this long ass list of stuff means, but if you are willing to read this and help me understand what it means I'd appreciate it. I occasionally compile to see if anything will come out wrong and this is what I got.

Here's the code it concerns:
QuadTree.h
#pragma once

#include "stdafx.h"
#include "VisibleGameObject.h"

enum Quadrant { NONE = 0, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, };
enum NodeType { BRANCH = 99, LEAF };

class QuadTreeNode;

typedef std::set<VisibleGameObject*> ObjectSet;
typedef std::set<QuadTreeNode*> NodeSet;

class QuadTree
{
public:
	QuadTree( float width, float height, unsigned int maxObjects );
	QuadTreeNode* CreateNode( float x, float y, float w, float h, Quadrant quad, QuadTreeNode* parent = NULL );
	void Divide( QuadTreeNode* parent );
	void TransferListDown( QuadTreeNode* parent, QuadTreeNode* child );
	void TransferListUp( QuadTreeNode* parent, QuadTreeNode* child );
	bool CheckNodeBounds( VisibleGameObject* object, QuadTreeNode* node );
	void Update( float elapsedTime );

protected:
	float _width;
	float _height;
	unsigned int _maxObjects;
	ObjectSet CollideList;
	NodeSet NodeList;
};




QuadTree.cpp
#include "stdafx.h"
#include "Quadtree.h"
#include "QuadTreeNode.h"


QuadTree::QuadTree( float width, float height, unsigned int maxObjects ) : _width( width ),
	_height( height ), _maxObjects( maxObjects )
{
}

QuadTreeNode* QuadTree::CreateNode( float x, float y, float w, float h, Quadrant quad, QuadTreeNode* parent )
{
	if( parent != NULL )
	{
		if( parent->_nodeType == NodeType::BRANCH )
		{
			QuadTreeNode* newNode = new QuadTreeNode( x, y, w, h, quad, NodeType::LEAF, parent );
			return newNode;
		}

		if( parent->_nodeType == NodeType::LEAF )
		{
			QuadTreeNode* newNode = new QuadTreeNode( x, y, w, h, quad, NodeType::LEAF, parent );
			parent->_nodeType = NodeType::BRANCH;
			return newNode;
		}
	}

	else
	{
		QuadTreeNode* newNode = new QuadTreeNode( x, y, w, h, quad, NodeType::BRANCH, NULL );
		return newNode;
	}	
}

void QuadTree::Divide( QuadTreeNode* parent )
{
	// Get the parent node width, height, x and y
	float w = parent->_width;
	float h = parent->_height;
	float x = parent->_posX;
	float y = parent->_posY;

	// Top Left
	QuadTreeNode* child =  CreateNode( x, y, w * 0.5f, h * 0.5f, Quadrant::TOP_LEFT, parent );
	bool exists = NodeList.find( child ) != NodeList.end();

	if( !exists )
	{
		NodeList.insert( child );
		TransferListDown( parent, child );
	}

	else
		delete child;

	// Top Right
	child = CreateNode( x + ( w * 0.5f ), y, w * 0.5f, h * 0.5f, Quadrant::TOP_RIGHT, parent );
	exists = NodeList.find( child ) != NodeList.end();

	if( !exists )
	{
		NodeList.insert( child );
		TransferListDown( parent, child );
	}

	else
		delete child;

	// Bottom Left
	child = CreateNode( x, y + ( h * 0.5f ), w * 0.5f, h * 0.5f, Quadrant::BOTTOM_LEFT, parent );
	exists = NodeList.find( child ) != NodeList.end();

	if( !exists )
	{
		NodeList.insert( child );
		TransferListDown( parent, child );
	}

	else
		delete child;

	// Bottom Right
	child = CreateNode( x + ( w * 0.5f ), y + ( h * 0.5f ), w * 0.5f, h * 0.5f, Quadrant::BOTTOM_RIGHT, parent );
	exists = NodeList.find( child ) != NodeList.end();

	if( !exists )
	{
		NodeList.insert( child );
		TransferListDown( parent, child );
	}

	else
		delete child;
}

void QuadTree::TransferListDown( QuadTreeNode* parent, QuadTreeNode* child )
{
	// This function will transfer all objects from the parent list to the children's list
	for( ObjectSet::iterator x = parent->_CollideList.begin(); x != parent->_CollideList.end(); x++ )
	{
		if( CheckNodeBounds( (*x), child ) )
		{
			child->_CollideList.insert( x );
			parent->_CollideList.erase( x );
		}

		else
			continue;
	}
}

void QuadTree::TransferListUp( QuadTreeNode* parent, QuadTreeNode* child )
{
	
}

bool QuadTree::CheckNodeBounds( VisibleGameObject* object, QuadTreeNode* node )
{
	sf::Rect<float> rect;
	rect.Left = node->_posX;
	rect.Right = node->_posX + node->_width;
	rect.Top = node->_posY;
	rect.Bottom = node->_posY + node->_height;

	return rect.Intersects( object->GetBoundingRect(), &rect );
}




Here's the Error:

Quote

>c:\program files\microsoft visual studio 10.0\vc\include\xmemory(208): error C2440: 'initializing' : cannot convert from 'std::_Tree_const_iterator<_Mytree>' to 'VisibleGameObject *'
1> with
1> [
1> _Mytree=std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\program files\microsoft visual studio 10.0\vc\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<std::_Tree_const_iterator<_Mytree>&>(VisibleGameObject **,_Other)' being compiled
1> with
1> [
1> _Ty=VisibleGameObject *,
1> _Mytree=std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>,
1> _Other=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>> &
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(592) : see reference to function template instantiation 'void std::_Cons_val<std::allocator<_Ty>,_Ty,std::_Tree_const_iterator<_Mytree>&>(_Alloc &,_Ty1 *,_Ty2)' being compiled
1> with
1> [
1> _Ty=VisibleGameObject *,
1> _Mytree=std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>,
1> _Alloc=std::allocator<VisibleGameObject *>,
1> _Ty1=VisibleGameObject *,
1> _Ty2=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>> &
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(755) : see reference to function template instantiation 'std::_Tree_nod<_Traits>::_Node *std::_Tree_val<_Traits>::_Buynode<std::_Tree_const_iterator<_Mytree>&>(_Valty)' being compiled
1> with
1> [
1> _Traits=std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>,
1> _Mytree=std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>,
1> _Valty=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>> &
1> ]
1> f:\users\GC\documents\visual studio 2010\projects\pang tutorial\pang tutorial\quadtree.cpp(104) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::_Tree_const_iterator<_Mytree>&>(_Valty)' being compiled
1> with
1> [
1> _Ty1=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>>,
1> _Ty2=bool,
1> _Traits=std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>,
1> _Mytree=std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>,
1> _Valty=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<VisibleGameObject *,std::less<VisibleGameObject *>,std::allocator<VisibleGameObject *>,false>>> &
1> ]


From what I gather there is something wrong with the iterator I used to iterate through my ObjectSet. I've used this technique before in a different project, but for some reason I get this long winded error that I lack the knowledge to make heads or tails of. Either that or there is a problem with the call
CheckNodeBounds( (*x), child );



On QuadTree.cpp line 102, if so, I can't see why there is a problem, as I have done this before as well and it worked.

Is This A Good Question/Topic? 0
  • +

Replies To: Iterator Conversion problem?

#2 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Iterator Conversion problem?

Posted 21 December 2012 - 12:44 AM

It might be that the iterator is constant and the function parameter is not.

Try promising not to change the object

bool CheckNodeBounds( VisibleGameObject* const object, QuadTreeNode* node );



I think that would be the right place for const.

Const correctness
Was This Post Helpful? 0
  • +
  • -

#3 GryphonClaws   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: Iterator Conversion problem?

Posted 21 December 2012 - 01:12 AM

Oh! Didn't even know any of this stuff. Thanks for that link!
Was This Post Helpful? 0
  • +
  • -

#4 GryphonClaws   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: Iterator Conversion problem?

Posted 21 December 2012 - 02:17 AM

if( CheckNodeBounds( (*x), child ) )
{
           child->_CollideList.insert( x );
           parent->_CollideList.erase( x );
}


The problem was also here, I need to pass x as (*x) to the insert and erase functions. Completely missed that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1