Jump to content
  • Advertisement
Sign in to follow this  
m4gnus

How to use STL-list?

This topic is 4896 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

Hi I read some tutorials on STL as many people on this forum say it's faster than the most selfwritten container classes and somehow i can't even declare a list in VC .net 2003...the compiler says: Compiling... VertexManager.cpp c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(33) : error C2143: syntax error : missing ';' before '<' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(33) : error C2501: 'CVertexManager::list' : missing storage-class or type specifiers c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(33) : error C2238: unexpected token(s) preceding ';' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(34) : error C2143: syntax error : missing ';' before '<' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(34) : error C2501: 'CVertexManager::list' : missing storage-class or type specifiers c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(34) : error C2238: unexpected token(s) preceding ';' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(35) : error C2143: syntax error : missing ';' before '<' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(35) : error C2501: 'CVertexManager::list' : missing storage-class or type specifiers my code:
class CVertexManager {
private:
	list<VertListEntry> VertList;
	list<VertGroup>     VertGroup;
	list<int>           TextureChange;
   
	CVertexManager();
public:
	~CVertexManager();
	void addEntry(VertListEntry Entry);
	
	static CVertexManager& Instance() {
		static CVertexManager TheOneAndOnly;
		return CVertexManager;

	}

};

btw what does the list.insert function want as the first parameter? it says something with iterator...what is that? can i just pass an int as the number where i want to have the new entry? regards, m4gnus

Share this post


Link to post
Share on other sites
Advertisement
you need to include the <list> header and, since the standard library declares everything to be in the std namespace you need let the compiler know that the list you are refering to comes from std:


#include <list>

class CVertexManager {
private:
std::list<VertListEntry> VertList;
std::list<VertGroup> VertGroup;
std::list<int> TextureChange;
CVertexManager();
public:
~CVertexManager();
void addEntry(VertListEntry Entry);

static CVertexManager& Instance() {
static CVertexManager TheOneAndOnly;
return CVertexManager;
}
};




Usually in a header you would use an explicit qualification. It is unfriendly to put using std::list; or using namespace std; which drags in the name for everyone using your header. Be explicit.

Share this post


Link to post
Share on other sites
std::list worked fine...thx but i still don't know how to use list.insert...

Edit: How to get the pointer to the object at the position x(int) in a list? Do you have to do it via list.begin()+x ? Does this actually work?

regards,
m4gnus

Share this post


Link to post
Share on other sites
std::list doesn't use random access iterators. You could use std::advance, but it will be slow because it will need to go through all the nodes before the one you want to find it. If you need random access, use a vector.

Also, iterators aren't directly convertable to pointers. Use &*iterator for that.

And an example of iterating through a list:


#include <iostream>
#include <list>

int main()
{
typedef std::list<int> list;
typedef list::iterator iterator;

list my_list;

my_list.push_back(1);
my_list.push_back(2);
my_list.push_back(3);

for(iterator i = my_list.begin(); i != my_list.end(); ++i)
std::cout << *i << std::endl;
}

Share this post


Link to post
Share on other sites
ok thx... this should also work shouldn't it?:
typedef std::list<int>::iterator intIterator;
typedef std::list<VertListEntry>::iterator vertIterator;
but i get this error:

c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.cpp(4): error C2440: 'initializing' : cannot convert from 'std::list<_Ty>::iterator' to 'std::list<_Ty>::iterator'
with
[
_Ty=VertListEntry
]
and
[
_Ty=int
]


which confuses me... " cannot convert from 'std::list<_Ty>::iterator' to 'std::list<_Ty>::iterator'" both expressions are the same so where is the problem??


regards,
m4gnus

Share this post


Link to post
Share on other sites
line4:
for(intIterator i=VertList.begin();i !=VertList.end();++i) {
btw what does ++i do? i++ increases i by 1 but i'm clueless about ++i...

regards,
m4gnus

Share this post


Link to post
Share on other sites
The ++ operator is overloaded by the list class. It simply moves the iterator to the next item in the list.

Just a note: i++ does not increase i by one. It does the same thing as ++i. Moves you to the next item in the list.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!