Jump to content
  • Advertisement
Sign in to follow this  
esperknight

Vectors combined with structures

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

I hope I'm not asking a stupid question but i'm trying to use vectors combined with contructors. A basic example from what I'm doing is: struct click_points { int x; int y; int max_x; int max_y; string default_image; string on_over; string function; }; vector<click_points> v_click_points; click_points temp_click_points; v_click_points.push_back(temp_click_points); cout << "vector = " << v_click_points[0].default_image; my code basically puts everything into the temp_click_points variable then pushes into the vector but no matter which element i access in the vector, it seems to return the y one. Am i pushing this incorrectly? I've been trying different ways to push it onto the vector by specifying which variable im accessing in the struct on either one but nothing is working. Any ideas or solutions are appreciated! Thanks! EsperKnight

Share this post


Link to post
Share on other sites
Advertisement
I see no reason why it should always "return the y one", even though I am a bit uncertain as what you mean by that. Can you post the actual code you are using, the results you get, and the results you expected?

Share this post


Link to post
Share on other sites
I don't see the problem either, but this...

Quote:
i'm trying to use vectors combined with contructors


... is a typo. You meant "structures" instead "constructors," right?

Share this post


Link to post
Share on other sites
Wow, structures is right, i even missed that rereading my code but i'll post what I'm doing here.


struct click_points
{
int x;
int y;
int max_x;
int max_y;
string default_image;
string on_over;
string function;
};




//This reads the pushed back data code and builds the menu
int Menu_Build_Display(vector<string> menu_data)
{
string temp;
int start, end;
start = 0;
end = 0;

int i = 0;

vector<click_points> v_click_points;
click_points temp_click_points;

start = menu_data.find("<FUNCT=");
end = menu_data.find(" ");

while (i < menu_data.size())
{
start = menu_data.find("<FUNCT=");
end = menu_data.find(" ");

if (start == -1)
{
//cout << "ERROR ERROR WILL ROBINSON! YOU ARE MISSING <FUNCT= TO START" << endl
// << "YOUR CODE! BEEP BEEP!" << endl;
}
else
{
start = start + 7;
end = end - start;
temp_click_points.function = menu_data.substr(start, end);

//v_click_points.push_back(temp_click_points);

start = menu_data.find("img=");
if (start > -1)
{
if ( (start = menu_data.find("./", start)) > -1)
{
start = menu_data.find("./", start) + 2;
}

end = menu_data.find(" ", start);
end = end - start;

temp_click_points.default_image = menu_data.substr(start, end);
}

start = menu_data.find("onover=");
if (start > -1)
{
if ( (start = menu_data.find("./", start)) > -1)
{
start = menu_data.find("./", start) + 2;
}
end = menu_data.find(" ", start);

end = end - start;
temp_click_points.on_over = menu_data.substr(start, end);
}

start = menu_data.find("x=");
if (start > -1)
{
start = start + 2;

end = menu_data.find(" ", start);
end = end - start;
temp = (menu_data.substr(start, end));
temp_click_points.x = atoi(temp.c_str());
}

start = menu_data.find("y=");
if (start > -1)
{
start = start + 2;

end = menu_data.find(" ", start);
if (end == -1)
{
end = menu_data.find(">", start);
}
end = end - start;
temp = (menu_data.substr(start, end));

temp_click_points.y = atoi(temp.c_str());

}


v_click_points.push_back(temp_click_points);

cout << "vector = " << v_click_points[0].y;
}
i++;
}

return 0;
}




An example of the script it reads in is (there are quite few more that go through this) :
<FUNCT=#ToC img=./Graphics/Interface/start.bmp onover=./Graphics/Interface/startGlow.bmp x=70 y=295>

it does read it in fine, but trying to push back the temporary structure does not produce anything in the vector. I tried just now outputting it. I can output the y value using cout << "vector = " << v_click_points[1].y; will return 295 but using cout << "vector = " << v_click_points[0].y; will return 0 (which should actually return 295) cout << "vector = " << v_click_points[0].default_image; returns nothing. Hope this helps explain it a bit better. Thanks!

EK

Share this post


Link to post
Share on other sites
Quote:
Original post by esperknight
I can output the y value using

cout << "vector = " << v_click_points[1].y; will return 295

but using

cout << "vector = " << v_click_points[0].y; will return 0

(which should actually return 295)

cout << "vector = " << v_click_points[0].default_image; returns nothing.

Hope this helps explain it a bit better. Thanks!

EK


Yes, it looks to me like you are inserting a bad "click point" prior to the one you really want. Double check v_click_points.size(). Print the contents of temp_click_point prior to inserting it in the vector.

Additionally, passing menu_data by value is inefficient: try int Menu_Build_Display(const vector<string>& menu_data) instead.

You may also use a string reference instead of all those menu_data:

while (i < menu_data.size())
{
const string& data = menu_data

start = data.find("<FUNCT=");
end = data.find(" ");

...

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!