[SOLVED] Can't get std::ostringstream* back

Hi,

I want a npc who gives a list of all the random propreties for a specific item id in a gossip menu. (with strings in ItemRandomProperties.dbc)

Something like :

  • … of the wolf

  • … of the boar

  • … of agility

So I put those strings in a std::ostringstream*.

Here is my code :

in ItemEnchantmentMgr.h :

std::ostringstream* rand_item_suffix(Player *player, int32 item_id);

in ItemEnchantmentMgr.cpp :

[CODE]std::ostringstream* rand_item_suffix(Player *player, int32 item_id) {

uint32 count = 0;

std::ostringstream rand_item_suffix[40];

[…]

for (EnchStoreList::const_iterator ench_iter = tab->second.begin(); ench_iter != tab->second.end(); ++ench_iter) {

const ItemRandomPropertiesEntry* random_id = sItemRandomPropertiesStore.LookupEntry(ench_iter->ench);

if(count <= 35)

rand_item_suffix[count] << random_id->nameSuffix[2];

count++; }

sLog->outErrorDb(“before : %s”, rand_item_suffix[0].str().c_str());

return rand_item_suffix;

[/code]

in my custom code :

[/CODE][code]std::ostringstream* r_i_s = (rand_item_suffix(player, item_id));

sLog->outErrorDb(“after : %s”, r_i_s[0].str().c_str());[/code]
logs :

[code]before : of agility

after :[/code]
Thanks in advance.

Edit, bump.

I don’t understand why the string doesn’t stay.

Please can someone help me ?

Your array just gets unallocated.

http://en.wikipedia.org/wiki/Scope_(computer_science)

I really try to understand. I don’t know what I have to change, I tried a lot of thing (days and weeks !), and that’s weird, when I don’t use an array, but just a single string it works !

I’m not a professional, yet, I will learn all that, but now I need my npc more. >< Can you tell me what I have to change ? Please… It will help so much.

Your array was a local variable, local variables are deleted upon method exit, you returned a pointer to your array’s first element ( which is valid, because basically “type name[X]” is the same as “type* name”), but when you tried to access it outside your function, it was already deleted.

Solved, thanks a lot.

[SPOILER]I didn’t know how to keep that array, finally juste changed :

std::ostringstream* rand_item_suffix(Player *player, int32 item_id) {

std::ostringstream rand_item_suffix[40];

into that : ><

std::ostringstream rand_item_suffix[40];

std::ostringstream* rand_item_suffix(Player *player, int32 item_id) {

[/SPOILER]

It’s still terribly bad implementation. Crash waiting to happen.

I really don’t know what I have to do…

Tell me. ><

(for now it works.)