Question about the list push_back function

First off, I'd like to point out I am new to the forums here so if you must bash my ignorance, please do so gently.

This question is related to a problem I'm having with my assignment. I kind of don't want to post code because I don't want someone to fix my code for me -- instead I have a question that might help me understand whats going on. Basically, I'm reading information from a text file and using that info as arguments for creating a new class instance to push_back into a list. My code worked when I used a vector and with a DList (an example doubly linked list created by my instructor). But as soon as I switched to a list, push_back fails and my code breaks because I attempt to change a garbage value later in the code.

So my question is: is there something different about the push_back function between vector and list that would prevent it from pushing a class object?

Any help is appreciated.

Thanks -
Jake M.
Seeing the code will help in diagnosing the problem, but if you're uncomfortable with that, you could just give us an example of the issue.
And what is the exact error you are getting?
Well this is essentially a copy of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
typedef System::ConsoleColor SCC

ifstream ifs("info.txt");
if(ifs.is_open())
{
	string text, name;
	int fg, bg, left, top;

	while (true)
	{
		getline(ifs, text, '\t');
		if(ifs.eof())
			break;

		ifs >> fg;
		ifs >> bg;

		ifs.ignore(INT_MAX, '\t');
		getline(ifs, name, '\t');

		ifs >> left;
		ifs >> top;

		foo.push_back(new object(text.c_str(), (SCC)fg, (SCC)bg, name.c_str(), left, top));
	}
	ifs.close();		
}

(*foo.begin() + 1)->SetName(RandomName());


Right now it breaks at the call to SetName() because the invoking object doesn't exist and I get an error saying something along the lines of "attempted to read or write into protected memory".
Last edited on
"class" cannot be an identifier. You sure this isn't C++/CLI?
Last edited on
I'm sorry; I'm not using it as an identifier.. I'll fix that. Not sure what CLI is. This is a c++ project.
Last edited on
"C++/CLI" is Microsoft's convoluted version of C++. I'm really glad you're not using it ;)

So I have to wonder:
(*foo.begin() + 1)->SetName(RandomName());
This makes me think that whatever (*foo.begin()) returns is a pointer, then you add one to it, then you call the function on it.

maybe you meant:
(*(foo.begin() + 1))->SetName(RandomName());
That gives an error saying "no operator + matches these operands"
Then you would need something like
1
2
3
4
5
{
    list<object>::iterator it = foo.begin();
    ++it;
    (*it)->SetName(RandomName());
}

where object is the type of the elements in the list.
Woah, now I'm getting somewhere... So obviously foo.begin() doesn't return what I needed it to. I'm going to look into this iterator thing.
Thanks guys. This definitely has me pointed in the right direction to fix my code and something I'll look out for in the future.
Topic archived. No new replies allowed.