通过指针获取公共类成员时的分段错误

时间:2016-09-11 17:45:35

标签: c++ pointers segmentation-fault c++14

我有一些课程可以处理玩家在游戏中选择的区域。

Region类:

class Region 
{
public:
    string name;

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

玩家控制器类:

class PlayerController
{
public 
    Region* playerRegion
}

RegionHandler类:

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];
    }
}

主:

int main() 
{
    RegionHandler regionHandler;
    PlayerController playerController;

    regionHandler.chooseRegion();

    string mystr = playerController.playerRegion->name;

}

但是当我试图获得玩家选择的区域名称或者cout中我应该能够做的时候,即使名字是一个应该在RegionHandler中设置的公共字符串,我也会遇到分段错误。我没有其他错误,程序工作正常。

我输入playerRegionIndex的数字并且由string mystr = playerController.playerRegion->name;

引起后发生分段错误

我之前从未遇到过分段错误,我也不太了解GDB输出,这就是我问这个问题的原因。

GitHub

上的完整来源

使用g++ main.cpp -std=c++1y -o main -g

在新的Ubuntu Bash for Windows上编译

GDB输出:

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

1 个答案:

答案 0 :(得分:1)

像Igor所说,主playerController中的局部变量不受局部变量regionHandler的影响。我还在学习OOP,所以如果我错了,请更正我,但是playerController的区域是由regionHandler.chooseRegion()方法设置的。这是两个类之间的依赖关系。

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];
    }
}