Question

So I managed to solve my compiler problems and proceed with my project. I managed to get a rudimentary version working to verify that the core algorithm is logically sound, but I ran into a problem with unordered_map when I cleaned up the main function and installed input verification to avoid infinite loops. While the code compiles without error, it does not create the three unordered_maps that are necessary for the program to run properly. I tried searching for answers, but couldn't find anything helpful. I'm assuming that this a problem stemming from my less than solid understanding of OOP, so I'll walk you through the progression of the unordered_maps in the problem so that it should be obvious what the error is. This is my first program where I've had to construct the classes and main function (my university class gave us the classes and main; we wrote the methods), so I'm struggling a bit with how to write a true OO program. I'll include a brief description above each code block.

Class declaration of main object class:

class world{
private:
    type *types;  //previously declared class
    typepermutation *typepermset; //previously declared class
    unordered_map <string,int> typetable; //this and the following two lines are supposed to declare the three maps
    unordered_map <int,string> revtypetable;
    unordered_map <int,string> efftable;
public:
    world();
    unordered_map <string,int> gentypetable(); //these three declare the functions constructing the individual maps
    unordered_map <int,string> genrevtypetable();
    unordered_map <int,string> genefftable();
    void readin();
    void gentypeperm();
    void gentables(); //this is the function intended to construct all three tables simultaneously in main()
    int* gatherattacks(int atkmov);
    int* convertstring(int atkmov, string* atktypes);
    void analyzeattacks(int atkmov, int* atktypesint);
    void printanalysis(int atkmov, int* ams, int* totals);
};  

Function methods (individual); omitted lines for brevity since the tables are large:

unordered_map <string,int> world::gentypetable(){
    unordered_map <string,int> hashtable;
    hashtable.emplace("Normal",0);
        ...//omitted lines
    return hashtable;
}

unordered_map <int,string> world::genrevtypetable(){
    unordered_map <int,string> hashtabletri;
    hashtabletri.emplace(0,"Normal");
    ... //omitted lines
    hashtabletri.emplace(35,"normal");
    return hashtabletri;
}

unordered_map <int,string> world::genefftable(){
    unordered_map <int,string> hashtabledva;
    hashtabledva.emplace(0,"Quadruple Effective");
    ... //omitted lines
    return hashtabledva;
}

Joint construction function:

void world::gentables(){
    unordered_map <string,int> typetable = gentypetable();
    unordered_map <int,string> revtypetable = genrevtypetable();
    unordered_map <int,string> efftable = genefftable();
}

Main:

int main(){
    int atkmov,i,n;
    char filename[255],ans='y';
    world myworld;
    cout << endl << "intro text" << endl << endl;
    myworld.readin();
    myworld.gentypeperm();
    myworld.gentables(); //call function to create maps
    do {
        cout << "Enter the number of unique moves: ";
        cin >> atkmov;
        cout << endl;
        int *atktypesint = myworld.gatherattacks(atkmov); //this is a function that prompts the user to input the types of moves
        myworld.analyzeattacks(atkmov,atktypesint);
        cout << "Analyze another moveset? (y/n): ";
        for (;;){
            cin >> ans;
            if ((ans == 'y') || (ans == 'n'))
                break;
            else{
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Please enter either 'y' or 'n' to proceed: ";
            }

        }
        cout << endl;
    } while (ans != 'n');
    return 1;
}       

The problem becomes immediately obvious when the myworld.gatherattacks() is called, though. To avoid an input that isn't a real type, I check each input against the strings stored in revtypetable<>; I loop through each integer value from 0 to 35 and see if the input string matches a stored string. However, even when I enter Normal (making well sure to type it correctly), it fails. Obviously the tables aren't being made properly, but I have no idea why. My understanding was that you can declare data members of a class and then use constituent functions to fill them with values; indeed, that's exactly what I do with myworld.gentypeperm() (it fills the declared typepermutation array), and I know that works successfully.

I know this is a lengthy question, but I feel like the answer is relatively simple for someone with better knowledge of the language. Any pointers towards the solution would be greatly appreciated.

Was it helpful?

Solution

tl;dr - if you declare

unordered_map <string,int> typetable = gentypetable();

inside a local scope, it will hide the member and create a new local variable with that name in the method. Replace it (and others) with

typetable = gentypetable();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top