-2

I have a few classes that handle setting a player's chosen region in a game.

The Region class:

class Region 
{
public:
    string name;

    Region(string n) : name(n) {}
}

The player controller class:

class PlayerController
{
public 
    Region* playerRegion
}

The RegionHandler class:

class RegionHandler 
{
public:

    enum RegionCode { EU, RU, SEA, AM, CH, AF};

    PlayerController playerController;

    vector<Region> regions;

    RegionHandler() {
        static const char *names[] = { "russia", "sea", "america", "china", "africa"};
        for (auto n : names)
            regions.emplace_back(n);
    }


    void chooseRegion() {

        int playerRegionIndex;

        cout << "Regions:\n"
             << "0. Europe\n"
             << "1. Russia\n"
             << "2. SEA\n"
             << "3. America\n"
             << "4. China\n"
             << "5. Africa\n";

        do {
            cout << "Select a region\n> ";
            cin >> playerRegionIndex; // Get user to input a region int
        }

        while(playerRegionIndex < 0 || playerRegionIndex > 5);

        playerController.playerRegion = &regions[playerRegionIndex];
    }
}

Main:

int main() 
{
    RegionHandler regionHandler;
    PlayerController playerController;

    regionHandler.chooseRegion();

    string mystr = playerController.playerRegion->name;

}

But when I try to get the name of the region the player has chosen into a string or in cout which I should be able to do, I get a segmentation fault even though name is a public string which should be set in RegionHandler. I get no other errors and the program works fine otherwise.

The segmentation fault occurs after I enter a number for playerRegionIndex and is caused by string mystr = playerController.playerRegion->name;

I have never come across a segmentation fault before and I don't really understand the GDB output, which is why I am asking this.

Full source on GitHub

Compiled on the new Ubuntu Bash for Windows using g++ main.cpp -std=c++1y -o main -g

GDB Output:

Program received signal SIGSEGV, Segmentation fault.
0x00007f6f237ab45b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
6
  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger. Sep 11, 2016 at 17:47
  • 6
    regionHandler.chooseRegion() sets up regionHandler.playerController. The local variable in main, also named playerController, is unaffected, its playerRegion member remains uninitialized, containing garbage. Also note that you have 6 RegionCodes but only 5 elements in regions vector. Sep 11, 2016 at 17:53
  • @πάνταῥεῖ I'm not sure you have read my question. I have provided the full source of the program which is compilable and all of the relevant lines of code that could be combined into a main.cpp and compiled to work fine. I have given the gdb output and explained that I do not understand it very well. I have been as concise as possible in my code and I haven't just dumped it asking for people to debug it for me. I do not understand what is causing the problem and that is why I have asked the question.
    – exitcode
    Sep 11, 2016 at 17:56
  • Possible duplicate of segmentation fault accessing a private class variable
    – Paul Roub
    Sep 11, 2016 at 18:55
  • @PaulRoub the only similarity I can see between the two questions is the fact that both yield a segmentation fault. The issue here is caused by a bad dependency relationship between two classes as both JJTO and Igor have pointed out. None of the answers on the question you have linked as a possible duplicate do anything to help answer this question.
    – exitcode
    Sep 11, 2016 at 19:13

1 Answer 1

1

Like Igor said, local variable in main playerController is not being affected by local variable regionHandler. I'm still learning OOP so correct me if I'm wrong but playerController's region is set by regionHandler.chooseRegion() method. This is a dependency relationship between the two classes.

class PlayerController
{

private:

public 
    Region* playerRegion;
    PlayerController()
    {...
      RegionHandler handle;
      string region = handle.getRegion(); //using RegionHandler class's method
      playerRegion = new Region(region); 
      ...
    }

}

class RegionHandler 
{
public:

    enum RegionCode { EU, RU, SEA, AM, CH, AF};

    PlayerController playerController;

    vector<Region> regions;

    RegionHandler() {
        static const char *names[] = { "russia", "sea", "america", "china", "africa"};
        for (auto n : names)
            regions.emplace_back(n);
    }


    string chooseRegion() { //change the return type to string

        int playerRegionIndex;

        cout << "Regions:\n"
             << "0. Europe\n"
             << "1. Russia\n"
             << "2. SEA\n"
             << "3. America\n"
             << "4. China\n"
             << "5. Africa\n";

        do {
            cout << "Select a region\n> ";
            cin >> playerRegionIndex; // Get user to input a region int
        }

        while(playerRegionIndex < 0 || playerRegionIndex > 5);

       return &regions[playerRegionIndex];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.