Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

STL Mapping Container C++ :(

  • 01-06-2005 1:35pm
    #1
    Closed Accounts Posts: 110 ✭✭


    I want to create a new dynamic class at runtime and group this to a word and then store it in a STL MAP only thing is in the for loop the original value that I load into the map keeps getting over written so in pseudo code
    Wordclass is a class loaded from a header file.


    #Include <map>
    using namespace std;


    map<wordclass,char> stufftostore;
    typedef group<wordclass,char> group;
    .
    .
    .
    Map<wordclass,Char>

    ///so start loop until ctrl+d is pressed While(!Cin.eof())

    Char happydays[10]; create string
    wordclass *WD; pointer to class

    WD = new wordclass(some_info_for_the_class); set pointer to class
    Cin.getline(happydays,9); get word for map

    group values(WD,happydays);
    //group
    stufftostore.insert(p1);
    //boot into STL MAP


    Now the problem is the address of WD is always the same and when I run a function to display what is in the map it only outputs the 1 value ie the last 1 inputted. I used to be quite good at maps and cant understand where this is coming from. I don’t know if I should be deleting the WD of if I have some how done this wrong. Could some 1 explain how exactly I should be doing this. Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 3,312 ✭✭✭mr_angry


    Ok, I think your terminology is a little off for starters - the word "class" really only refers to the template for "objects". Classes cannot normally be created at run-time in C++ (unless you do some serious tricking around). What you are doing here is creating a new instance (or object).

    Secondly, if you have the ability to change the "worldclass" class definition, I would heartily recommend that you include the "word" as a member variable of the class and set it accordingly, in which case you can avoid using STL Maps altogether.

    Now, as for the actual problem you are having. You look like you are inserting "p1" into your Map, but you don't explain where p1 comes from, unless you've skipped over it with "//group". That function should automatically increase the size of the map and append the object to it. Likewise, the "new" keyword should automatically allocate memory and a new address, so it is unlikely that this is the problem.

    Its hard to tell from the psuedocode, but I would check that the line:
    Cin.getline(happydays,9); get word for map
    ...is definitely overwriting the value in "happydays". Otherwise, that is a likely explanation for the value being the same. Either that, or your loop is only executing once.

    If nothing becomes apparent after checking both of those, I'd advise pasting the actual code itself here, because at the moment, its hard to determine what you are doing, why, and how.


  • Closed Accounts Posts: 110 ✭✭Adblock


    hey

    thanks for the reply mr_angry; but i managed to resolve the problem after 10 minuter of posting. i mean im looking at the think for a week reading loads and then as soon as i post bang the solution hits me..

    if you interested the problem was

    i had a function to add in all the class stuff based on querys..but i wasnt returning the correct class from the function really stupid mistake :(

    thanks again for your responce.

    Cheers :)


Advertisement