【问题标题】:Make a collection of an abstract class type, Abstract Class vector of shared_ptr做一个抽象类类型的集合,shared_ptr的抽象类向量
【发布时间】:2019-09-05 23:06:34
【问题描述】:

错误

e/c++/v1/算法:642: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:321:9: 错误:

字段类型'Space'是一个抽象类 _T2秒; ^

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:624:16:注意:

问题

如何定义Space 类型的std::vector,它是一个抽象类,然后用派生类EmptySnakeLadder 的实例填充这个向量。

上下文

我知道 C++ 中的抽象类不能被实例化。相反,我在此站点和其他站点上的几篇文章中读到,如果该类型定义为星形 * 指针或任何 <memory> 托管指针数据类型(如 @987654328),则可以创建抽象类型的集合@。在我的情况下,我尝试使用shared_ptr<Space>,但仍然无法正确定义集合。我使用g++ -std=c++17 main.cpp && ./a.out 编译了我的代码。

代码

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <typeinfo>
#include <queue>
#include <string>
#include <vector>

class Player
{
    private:
        int m_current_space = 1;
    public:
        Player() {}
        void role_dice() {
            m_current_space += floor( (rand()%10 + 1) / 3 );
        }
        int const get_current_space() {
            return m_current_space;
        }
        void set_current_space(int current_space) {
            m_current_space = current_space;
        }
};

class Space
{
    protected:
        int m_id;
        std::vector<Space> m_paths;
    public:
        Space() {} // requied to use [] operator in map
        Space(int id) : m_id(id) {}
        void add_path(Space& s) {
            m_paths.push_back(s);
        }
        int get_id() {
            return m_id;
        }
        virtual std::string class_type() = 0;
};
class Empty : public Space
{
    public:
        Empty(int id) : Space(id) {}
        std::string class_type() {
            return "Empty";
        }
};
class Ladder : public Space
{
    public:
        Ladder(int id) : Space(id) {}
        virtual void event(Player& p) {
            p.set_current_space(1);
        }
        std::string class_type() {
            return "Ladder";
        }
};
class Snake : public Space
{
    public:
        Snake(int id) : Space(id) {}
        virtual void event(Player& p) {
            p.set_current_space(4);
        }
        std::string class_type() {
            return "Snake";
        }
};

class Board
{
    private:
        std::map<int, Space> m_board;
    public:
        void add_space(Space& s) {
            m_board[s.get_id()] = s;
        }
        void draw_board() {
            int i = 1;
            for(auto const& [space_key, space] : m_board) {
                if(i%3 == 0) {
                    std::cout << "○\n";
                }
                else if(typeid(space) == typeid(Snake)) {
                    std::cout << "○-";
                }
                else {
                    std::cout << "○ ";
                }
                ++i;
            }
        }
        void update_player_on_board(int position) {
            int i = 1;
            for(auto const& [space_key, space] : m_board) {
                if(i%3 == 0) {
                    if (space_key == position) {
                        std::cout << "●\n";
                    }
                    else {
                        std::cout << "○\n";
                    }
                }
                else if(typeid(space) == typeid(Snake)) {
                    std::cout << "○-";
                }
                else {
                    if (space_key == position) {
                        std::cout << "● ";
                    }
                    else {
                        std::cout << "○ ";
                    }
                }
                ++i;
            }
        }
        const std::map<int, Space> get_board() {
            return m_board;
        }
        friend std::ostream &operator<<(std::ostream& os, const Board& b) {
            return os;
        }
};

class GameStateManager
{
    private:
        std::string m_state = "game over";
        bool m_playing = false;
    public:
        std::string const get_state() {
            return m_state;
        }
        void set_state(std::string state) {
            m_state = state;
        }
};

int main()
{
    std::cout << "Welcome to Bowser's 9 board game\n";
    std::cout << "Start? y(yes) n(no)\n";

        GameStateManager game_manager;
        game_manager.set_state("playing");


        auto space1 = std::make_shared<Space>(1);
        auto space2 = std::make_shared<Space>(2);
        auto space3 = std::make_shared<Space>(3);
        auto space4 = std::make_shared<Space>(4);
        auto space5 = std::make_shared<Space>(5);
        auto space6 = std::make_shared<Space>(6);
        auto space7 = std::make_shared<Space>(7);
        auto space8 = std::make_shared<Space>(8);
        auto space9 = std::make_shared<Space>(9);

        std::vector<std::shared_ptr<Space>> v {
            space1, space2, space3,
            space4, space5, space6,
            space7, space8, space9
        };

        Board bowsers_bigbad_laddersnake;
        for(int i = 0; i < 10; ++i) {
            bowsers_bigbad_laddersnake.add_space(*(v[i]));
        }
        bowsers_bigbad_laddersnake.draw_board();

        Player mario;

        int turn = 0;
        while(game_manager.get_state() == "playing") {
            std::cin.get();
            std::cout << "-- Turn " << ++turn << " --" << '\n';
            mario.role_dice();
            bowsers_bigbad_laddersnake.update_player_on_board(mario.get_current_space());
            if (mario.get_current_space() >= 9) {
                game_manager.set_state("game over");
            }
        }

        std::cout << "Thanks a so much for to playing!\nPress any key to continue . . .\n";
        std::cin.get();



    return 0;
}

【问题讨论】:

  • 您不能创建(实例化)抽象类类型的对象。 (std::make_shared&lt;Space&gt;)。
  • 你只能拥有一组指向抽象类型(智能或其他)的指针。
  • 您只能将 派生 类型附加到这些指针(确切地说是具体派生类型)。
  • 没有。 std::make_shared&lt;Empty&gt;(1) 创建一个 Empty 对象,std::make_shared&lt;Space&gt;(1) 尝试创建一个 Space 对象但失败了,因为 Space 是一个抽象类型。
  • @DeviationN 你在我的SpaceBoard 类中是对的,其中使用抽象Space 类的集合,我也将这些更改为智能指针并且代码正在编译和正在运行!

标签: c++ vector abstract-class unique-ptr


【解决方案1】:

您似乎已经删除了很多代码来了解这里的详细信息。 有一个Space 指针(智能或原始)。实例化您想要的特定空间,使用Space 类型的指针指向它。示例std::shared_ptr&lt;Space&gt; pointerToSpace = std::make_shared&lt;Snake&gt; ("I'm a snake"); 现在,不失一般性,您可以仅使用指向空间pointerToSpace-&gt;class_type() 的指针打印内容(具体类型)。是的,您可以在一个容器中拥有一组 shared_ptrs。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多