Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ struct stl

Started by yves032784 May 1, 2004 at 11:23 AM 10 replies 700+ views
Original Post
yves032784
yves032784
how can i access my struct members in my c++ code which also uses stl?
#include <iostream>
#include <list>
#include <string>

using namespace std;

class packages
{
public:
	typedef struct package
	{
		list<string> packagenames;
	}package;
	
	typedef struct subcategoy
	{
		list<package> packages;
		list<string> subcategorynames;
	}subcategory;
	
	typedef struct category
	{
		list<subcategory> subcategories;
		list<string> categorynames;
	}category;
	
	list<category> categories;
	
	packages();
	bool populate_packages();
	

};
 
when i call:
string test="sadfsdaf";
categories.categorynames.push_back(test);
 
i get errors: *pack.cpp:63: error: 'class std::list >' has no member named 'categorynames' *pack.cpp:63: error: `currentString' undeclared (first use this function) *pack.cpp:63:123: missing terminating ' character where did i go wrong? Is this bad style? If so, how should i correct it? [edited by - Magmai Kai Holmlor on May 1, 2004 2:31:29 PM]
ToohrVyk
ToohrVyk
From what I''ve understood, what you''re trying to do can''t work. Here''s why :

Suppose YOU were the computer. You''re given a list of categories. Each category contains a list of categorynames.

Now, I ask you to put "sadfsadf" into the list of categorynames that''s inside the list of categories.

Problem is, since there''s one list of categorynames per category, and you''ve got a whole list of the latter, to which list of categorynames are you going to add this?

Or, to make it even more clear :

This is your list of categories :

{A, B, C, D, E}

So these are all categorynames lists :

A.categorynames
B.categorynames
C.categorynames
D.categorynames
E.categorynames

To which are you trying to add the string?

Perhaps if you could explain what you''re trying to do we could help you out.

Victor Nicollet, INT13 game programmer

evolutional
evolutional
The problem is more simple, it looks like you're not declaring your lists correctly.

struct category
{
std::list<std::string> categorynames;
};


is better

[edited by - downgraded on May 1, 2004 12:44:21 PM]
ToohrVyk
ToohrVyk
Downgraded : the forums have this habit of eating < ... > tags. If you look at his post by clicking on "edit", you''ll see his tags are ok.

Also, if you were looking for the compiler error (rather than the design error), it''s easier than that : "categories" is of type std::list< category >, and as such it does not have a "categorynames" member (and especially not one of type "std::list< std::string >").

Victor Nicollet, INT13 game programmer

yves032784
yves032784
ToohrVyk, you made me realize that i was adding things wrong...i''m still a bit of a c++/stl noob, so please bare with me

how would i add say ''categories[1].categorynames'' in proper c++/stl?
ToohrVyk
ToohrVyk
If you want to add it to the first category in the list of categories, you could do something like :

*(categories.begin( )).categorynames.push_back( ... )

categories.begin( ) gets an iterator - it''s like a pointer, only you have more control over it. Its type is "std::list< your type >::iterator" - which points to the first element of "categories".

Then, *(categories.begin( )) dereferences the iterator : it reads the object it''s pointing to. This means you now have your first object, and you can add things to its member variables.

This of course assumes that categories isn''t empty... you should check if it is, or you run the risk of having bugs. Two ways of doing this :

categories.empty( ) returns true if the list is empty
categories.begin( ) == categories.end( ) if the list is empty

To get the following elements, just :

std::list< stuff >::iterator i = categories.begin( );

then do :

++i; // this moves i so it points to the next element in the list

until :

i == list.end( ) //you have reached the end of the list. i does not point to an object anymore (list.end() is OUTSIDE the list).

Victor Nicollet, INT13 game programmer

yves032784
yves032784
er....
I tried

*(categories.begin( )).categorynames.push_back(previousPackageName);


as you said and i get this error:


error: 'struct std::_List_iterator<packages::category, packages::category&, packages::category*>' has no member named ' categorynames'



[edited by - yves032784 on May 1, 2004 3:09:57 PM]
ToohrVyk
ToohrVyk
Oops, sorry... I think that "*" was meant to be after the "("...
yves032784
yves032784
ToohrVyk, THANKS A MILLION for your help

before i go ONE last question...i tried to traverse through and add data to the list doing something like this:



list<category>::iterator i = categories.begin( );

for(i=categories.begin(); i!= cartegories.end(); i++)
{
i.categorynames.push_back(previousPackageName);
}


i tried everything, putting the * character in fron of the i iterator...just couldnt figure it out, here is the error i get:


error: ''struct std::_List_iterator<packages::category, packages::category&, packages::category*>'' has no member named '' categorynames''
**** Exited with status: 2 ***
JohnBolton
JohnBolton
BTW, you are using C++, remove the typedefs. They don''t do anything except double the number of symbols your compiler has to deal with.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
mattnewport
mattnewport
Try

for(list<category>::iterator i = categories.begin(); i != categories.end(); ++i)
{
i->categorynames.push_back(previousPackageName);
}

or

for(list<category>::iterator i = categories.begin(); i != categories.end(); ++i)
{
(*i).categorynames.push_back(previousPackageName);
}

Topic Locked

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

Sign in to reply to this topic.