Skip to main content
gamedev.net
🔒 Locked

std::list::iterator [SOLVED]

Started by Lipponen 2 replies 1.2k views
Original Post
Lipponen
Lipponen
I have a big problem, a problem I cannot solve. This function breaks with the error below.

/*Fix: This function just explodes in my face!*/
template <class T>	T*   TComponent(uint32 type) {
	if(HasComponent(type)) {
		return static_cast<T*>((*m_Iter));
	}
	return NULL;
};

bool CEntity::HasComponent(uint32 type) {
	if(m_CompList.empty()) return false;

	m_Iter=m_CompList.begin();
	for( ; m_Iter!=m_CompList.end(); m_Iter++) {
		if((*m_Iter)->Type()==type)
			return true;
	}

	return false;
}



First-chance exception at 0x0041e86f in app.exe: 0xC0000005: Access violation reading location 0xcdcdcddd.
The iterator m_Iter is pointing to the right component at the return static_cast((*m_Iter)); part. But it gets the above error and blows up in my face. Ive tryied static_cast/reinterpret_cast/dynamic_cast, but with the same result. *BOOM* Im quiet desperate and out of options... [SOLVED] I made a function that returned a IComponent* and casted it manually while reciving it. It seems to work better. [Edited by - Lipponen on August 27, 2008 1:37:45 PM]
Bregma
Bregma
I don;t think you've posted enough code for us to guess what the problem is. Is TComponent(uint32) a member function of CEntity? Why doesn;t the posted code show that? If not, how does it have access to m_Iter?
Stephen M. Webb
Professional Free Software Developer
rip-off
rip-off
According to here:
Quote:

0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory


Is the Entity instance initialised?

Other than that, I am suspicious of you making the iterator a member. Why not:
struct Component{    virtual bool hasType(uint32) const = 0;    virtual void whatever() = 0;};typedef boost::shared_ptr<Component> ComponentPtr;struct HasType{    uint32 type;    HasType(uint32 type) : type(type) {}    bool operator()( const ComponentPtr &ptr ) const    {        return ptr->hasType(type);    }};class Entity{    typedef std::list<ComponentPtr> ComponentList;    ComponentList components;public:    template <class T>    T *component(uint32 type)    {    	ComponentList::iterator it = std::find_if(components.begin(),components.end(),HasType(type));        if(it != components.end())        {            Component *component = it->get();            return dynamic_cast<T *>(component);        }        return 0;    }    void add(ComponentPtr ptr) { components.push_back(ptr); }};struct UglyComponent : Component{    virtual bool hasType(uint32 type) const    {        return type == 1;    }    virtual void whatever()    {        std::cout << "I'm ugly :(\n";    }};struct PrettyComponent : Component{    virtual bool hasType(uint32 type) const    {        return type == 2;    }    virtual void whatever()    {        std::cout << "I'm pretty :D\n";    }};int main(){    Entity entity;    {        ComponentPtr component(new UglyComponent);        entity.add(component);        component.reset(new PrettyComponent);        entity.add(component);    }    for(uint32 i = 1 ; i < 4 ; ++i )    {        Component *component = entity.component<Component>(i);        if(component)        {            component->whatever();        }        else        {            std::cout << "No component: " << i << '\n';        }    }}

With some boost::bind hackery the "HasType" struct would be unnecessary.

This still has the problem that you are essentially writing your own RTTI on top of what C++ offers already. I mean, you are (essentially) passing both the template parameter to the function, and an integer, both of which (hopefully) refer to a particular type. This is potentially a brittle solution.
Lipponen
Lipponen
Sorry about that, was in a "hurry" to post so I forgot to write that the template function was inside the class header file.

I finally localized the problem to my functors, they were typedef:d all over the place over and over again. After fixin' them the template function works like it should have done from the start :-)

Thank you for the replies.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.