iter to address of item

Started by
22 comments, last by MaulingMonkey 16 years ago
hmm, can anyone tell me whats wrong the below code header

#ifndef UI_ITEM
#define UI_ITEM

#include "globalSettings.h"
#include "loadTexture.h"

class UI_item
{
public:
	UI_item(float posx, float posy, float posz, float scalex, float scaley, float scalez, 
		IDirect3DVertexBuffer9* VB, IDirect3DIndexBuffer9* IB, 
		char *imageLocation );
	~UI_item();

	void display();

private:

	D3DXMATRIX transform;
	IDirect3DVertexBuffer9* VB;		
	IDirect3DIndexBuffer9* IB;
	IDirect3DTexture9*     image;
	static std::vector <UI_item> UI_list;
};
#endif


cpp
#include "UI_Item.h"

extern IDirect3DDevice9* gd3dDevice;
std::vector <UI_item> UI_item::UI_list;	//declare the static member

UI_item::UI_item(float posx, float posy, float posz, float scalex, float scaley, float scalez, 
		IDirect3DVertexBuffer9* _VB, IDirect3DIndexBuffer9* _IB, 
		char *imageLocation )
{
	D3DXMATRIX statsWorld;
	D3DXMATRIX statsScale;
	D3DXMatrixIdentity(&statsWorld);
	D3DXMatrixIdentity(&statsScale);

	D3DXMatrixTranslation(&statsWorld, posx, posy, posz);
	D3DXMatrixScaling(&statsScale, scalex, scaley, scalez);
	transform = statsWorld * statsScale;
	VB = _VB;
	IB = _IB;
	loadTexture(gd3dDevice, imageLocation, &image);

	UI_list.push_back((*this));
	
}


UI_item::~UI_item()
{
}

void UI_item::display()
{
	std::vector <UI_item>::iterator iter = UI_list.begin();
	while (iter != UI_list.end())
		{
			gd3dDevice->SetTexture(0, iter->image);
			gd3dDevice->SetTransform(D3DTS_WORLD, iter->&transform);
			gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
			iter++;
		}

}


im getting the following error 1>d:\c++\my game\ui_item.cpp(37) : error C2059: syntax error : '&' which points to this line gd3dDevice->SetTransform(D3DTS_WORLD, iter->&transform);
Advertisement
Maybe you meant to write this:
gd3dDevice->SetTransform(D3DTS_WORLD, &(iter->transform));
If what you need to do is pass the address of the transform object as the 2nd param to SetTransform.

Note the brackets aren't necessary due to precedence of operators (& operator has lower precedence than ->), but used to illustrate the idea.
argh, sorry me being a fool again :(

thankyou,
iter->&transform is not legal syntax. I am assuming you are trying to pass the address of transform to gd3dDevice->SetTransform, in which case you need pass the address of the object - &(pointer->transform).

One problem you may encounter is that you are assuming std::vector <UI_item>::iterator is the same as UI_item* which may be the case on some compilers, but not on others. You need to dereference the iterator before you can use it to make it complient with all versions of the STL.

So you will probably want something like...
{  // Get the actual UI item that we are using  UI_item& uiItem = (*itor);  // More code..  gd3dDevice->SetTexture(0, uiItem.image);  gd3dDevice->SetTransform(D3DTS_WORLD, &(uiItem.transform));  // More code..}

This is because, depending on the version of the STL being used or the compiler settings, an iterator may not be a basic pointer, but another templated type (which allows for iterator checks and debugging for example).
SpreeTree, as far as I'm aware, most (if not all) iterators overload the -> operator to return a pointer to the current object in the same way they overload the * operator to return a reference to the current object.

So it should be completely equivalent to use either one.
std::vector<MyCustomPlayerClass> vec;vec.push_back(MyCustomPlayerClass("Player 1"));std::vector<MyCustomPlayerClass>::iterator it1;// Use a referencefor (it1 = vec.begin(); it1 != vec.end(); ++it1)    cout << (*it).GetName();// Use a pointerfor (it1 = vec.begin(); it1 != vec.end(); ++it1)    cout << it->GetName();
Quote:Original post by shurcool
SpreeTree, as far as I'm aware, most (if not all) iterators overload the -> operator to return a pointer to the current object in the same way they overload the * operator to return a reference to the current object.


You are correct, most implementations of the STL will over-ride operator->, but using is can lead people to believe that vector<>::iterator is a pointer when in-fact it is not. I have encountered a lot of people, new to STL, who assume that because an iterator behaves like a pointer, they can treat it as such.

By stressing that it isn't (and some implementations of the STL do enforce that), you can save a lot of time in the future when you start trying to treat an iterator as you would a pointer [edit] because you know iterators are not pointers, so you have a better chance of understanding why errors are occuring [/edit].

Also, getting into a habit of dereferencing an iterator makes it easier to use things like vector<MyObject*>::iterator, as you then have a more consistent code base when it comes to the STL.

[Edited by - SpreeTree on March 24, 2008 4:31:57 PM]
Quote:Original post by SpreeTree
You are correct, most implementations of the STL will over-ride operator->, but using is can lead people to believe that vector<>::iterator is a pointer when in-fact it is not.


All non-output iterators implement operator ->. It's required by the C++ Standard for input iterators, forward iterators, bidirectional iterators and random access iterators. By definition -> is usable on all STL container iterators. If you couldn't then they wouldn't be iterators.
Quote:Original post by SiCrane
All non-output iterators implement operator ->. It's required by the C++ Standard for input iterators, forward iterators, bidirectional iterators and random access iterators. By definition -> is usable on all STL container iterators. If you couldn't then they wouldn't be iterators.


Fair point, unfortunately I have often had to use STL implementations that haven't been fully implemented and unfortunatly not everything meets the standard as maybe it should. Regardless of whether it is implemented or not, the use of it does lead to issues, which I pointed out in my earlier post.

Really? So what compilers have you used that didn't implement operator-> on standard library container iterators?
Quote:Original post by shurcool
SpreeTree, as far as I'm aware, most (if not all) iterators overload the -> operator to return a pointer to the current object in the same way they overload the * operator to return a reference to the current object.


You are incorrect: the selection operator (operator->()) is supposed to select a member of the underlying typename value_type of the iterator. It does not return a pointer (unless of course the member of the underlying typename value_type is a pointer). All iterators provided by the standard library, where they provide that operator, provide that functionality.

The dereference operator (operator*()) returns an object of the underlying typename reference or typename const_reference. All iterators provide by the standard library, where they provide that operator, provide that functionality.

Not all iterator suppor the notion of a "current object." For example, a standard output iterator.

--smw

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement