Jump to content
  • Advertisement
Sign in to follow this  
PhiberOptic

STL / std::list problems

This topic is 4752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it even possible that an this code fails? Does anyone knows about strange behaivours by std::list? Please read the comment.

std::list<item> m_Items;

item i;
i.name = name;
i.type = type;

m_Items.push_back(i); // code crashes here with access violtaion 
//(well, actually in the file "list" and function iterator end() {return iterator(_Myhead));} after calling that push_back statement. 
//Of course that m_Items is declared as a memberfunction in a class. "item" is just a simple struct I've made. 
The class this function is in IS instanciated. So m_Items is created.

Share this post


Link to post
Share on other sites
Advertisement
Should be no problems.

Compile this:

#include <list>
#include <string>

struct item
{
std::string name;
unsigned int type;
};

int main( int argc, char* argv[] )
{
std::list< item > itemList;

item newItem;

newItem.name = "Dave";
newItem.type = 7;

itemList.push_back( newItem );
};




ace

Share this post


Link to post
Share on other sites
The stl containers require a valid copy constructor for any objects stored within them. If name is a char* rather than a string, I'd expect problems.

Share this post


Link to post
Share on other sites
you need to provide a copy constructor (and almost certainly assignment operator and destructor too).

You don't normally just assign a char* when copying a string and you shouldn't do that here. But that is what the default copy constructor will do, so you need to write your own.

Or use std::string which will automatically do the right thing

Share this post


Link to post
Share on other sites
Quote:
Original post by petewood
you need to provide a copy constructor (and almost certainly assignment operator and destructor too).

You don't normally just assign a char* when copying a string and you shouldn't do that here. But that is what the default copy constructor will do, so you need to write your own.

Or use std::string which will automatically do the right thing



I'm using std::strings and some int:s in the item. I don't see a need for a copyconstructor, am I wrong?

Share this post


Link to post
Share on other sites
Okay, here is the whole code that is relevant..

Where elsewhere can the fault be?


class CSConsole
{
// blah blah

private:
list<item> m_Items;

};


// then the function

void CSConsole::AddItem(string name,void* pItem,itemType type)
{
item i;
i.name = name;
i.type = type;
if( type == C_FUNCTION ) // Function
i.function = (c_function)pItem;
else // variabel
i.pVariabel = pItem;

m_Items.push_back(i);
}


Share this post


Link to post
Share on other sites
That code is almost identicle to the code you first posted, so I still can't see whats wrong. What we need to see is the item class declaration (and don't cut parts out, use [source][/source] tags to make it look nice.)

Somethings going wrong somewhere, and my guess would be the void *. With more code we may be able to offer a better solution based on templates or something.

Share this post


Link to post
Share on other sites
Okay, I solved the problem. You should really tripplecheck your asignments ;P
That function was called via a function, not directly via the pointer to the class.. and that function was fu*ed up.

Thanks to you all for taking your time with me anyway..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!