the assignment error??

Started by
7 comments, last by Conner McCloud 18 years, 4 months ago

typedef std::vector<Material*> MaterialSet;

std::vector<Mesh::MaterialSet> MeshManager::materialSetList()
{
	std::vector<Mesh::MaterialSet> t;
	MeshMapIterator iter;
    MeshMapIterator iend = m_meshList.end();
	int i = 0;
	for(iter = m_meshList.begin(); iter != iend; ++iter)
	{
		Mesh::MaterialSet temp = iter->second->materialSet();
		t[0] = temp;
		i++;
	}
	return t;
}



runtime error happen at t = temp; why?
Advertisement
In your code you have t[0] = temp;
You haven't set t's size. You can do this by using t.resize(count), or by using t.push_back(item) for each item if you don't know the final count. Using push_back() will cause t's internal array to be reallocated every so often (and waste some extra memory), so it's probably better to use resize() when you can.

Also, you code uses t[0], not t :)
Quote:Original post by hh10k
You haven't set t's size. You can do this by using t.resize(count), or by using t.push_back(item) for each item if you don't know the final count. Using push_back() will cause t's internal array to be reallocated every so often (and waste some extra memory), so it's probably better to use resize() when you can.

Alternatively, call t.reserve(count), and follow that with calls to push_back. That ensures no reallocations occur, and doesn't fill your vector with a bunch of useless default objects.

CM
Plus you're making a new temp every time you run through the loop. Might be a good idea to make it outside the for loop and then re-assign is every time.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
Plus you're making a new temp every time you run through the loop. Might be a good idea to make it outside the for loop and then re-assign is every time.

That variable has no business outside the scope of the for loop, so it has no business being declared outside the scope of the for loop. That's exactly the sort of trivial optimization you can count on the compiler doing for you if it has any merit.

CM
Quote:Original post by Conner McCloud
Quote:Original post by dbzprogrammer
Plus you're making a new temp every time you run through the loop. Might be a good idea to make it outside the for loop and then re-assign is every time.

That variable has no business outside the scope of the for loop, so it has no business being declared outside the scope of the for loop. That's exactly the sort of trivial optimization you can count on the compiler doing for you if it has any merit.

CM


Heh... Good point! But I never trust a compiler to optomize for me. I just try to code as good as possible, and maybe I'm lucky enough to have missed something! ;)
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
resize work.
But OFTEN I dont need to resize or reserve.forexample:
vector<int> v;
v = 4; just OK. Why?
Quote:Original post by derek7
resize work.
But OFTEN I dont need to resize or reserve.forexample:
vector<int> v;
v = 4; just OK. Why?

You're getting lucky. Maybe the vector is allocating a few bytes just in case. Maybe the vector's pointer holds junk data that happens to belong to you. The why doesn't matter. You always need to add elements to the vector before using [] to access those elements, whether the program runs that way or not.

CM

This topic is closed to new replies.

Advertisement