English 中文(简体)
用现有类设计实现函数的问题
原标题:Problem Implementing Function With Existing Class Design

我目前已经开始了我的编程课程的学期项目——编程一个完全自动驾驶模拟器。所有的汽车都通过人工智能操控,地图也打印在控制台上。

第一张表希望我们创建几个基本类:FCCompact(汽车)、Scanner(AI)、ID(地形)和World(地图)。

扫描仪目前在我的FCCompact类中被实现为“HAS-a”,如下所示:

// FCCompact.h
FC紧凑级
{
private:
    struct fImpl;
    fImpl *fImpl_;
/* ... */

// FCCompact.cpp
struct fImpl
{
    Scanner scanner_;
    /* ... */
};

有问题的扫描仪功能是

const ID& scanForID( int fovNumber,            // How far it can see
                     FCCompact::Direction dir, // Direction car is facing
                     const Location& loc);     // Location of car

到目前为止还不错。然而,整个地图位于容器向量<;deque<;身份证>>(1)在类World中;我不知道如何访问它。

到目前为止发布的函数是老师给我们的,所以它们应该能够在给定的参数下实现。我只是不知道该怎么做。

我的第一个想法是调用World::getID(Location&;),但它不是静态的。在使其成为静态后,它就不能再访问非静态成员了(嗯,我忘了)。我还制作了一个静态函数,它调用一个非静态函数;同样的问题(双重duh)。

然后我抛出了老师的指导方针,简单地传入了整个World对象,但这也没有真正起作用。

我能做什么?

请记住,这是技术上的家庭作业,我不想要完整、合格的答案,甚至可以让我完成我想做的事情。我想从这个项目中尽可能多地学习,所以请给我指明正确的方向。

(1) :容器类型的奇怪选择?我想,如果世界打印在控制台上,那么用矢量快速访问每一行将是有益的,因为不会插入或删除队列,而且访问时间也很长。

另一方面,deque可以方便地访问范围,这是有利的,因为我会经常改变相邻的列(沿着x轴或y轴行驶的汽车)。这一选择来得并不快;我仍然在这个、一个列表或一个简单的向量之间左右为难。

阶级世界

类ID;
阶级世界
{
    typedef std::pair<int, int> Location;

private:    
// vector<deque<ID> > map
// int mapHeight, mapWidth
// string mapName
    struct wImpl;
    wImpl *pimpl_;

public:
    World();
    ~World();

    void create(const std::string& name = "FC-City");

    int getHeight() const;
    int getWidth() const;

    ID getID(const Location& loc);

    friend std::ostream& operator<<(std::ostream& os, World& world)
    {
        return os;
    }

private:
    void loadMap(std::string name = "FC-City");

private:
    World(const World& other);
    World& operator=(const World& other);
};

类扫描仪

类ID;
类扫描仪
{
    typedef std::pair<int, int> Location;

public:
    Scanner();
    ~Scanner();

    const ID& scanForID( int fovNumber, 
                         FCCompact::Direction dir, 
                         const Location& loc);

private:
    Scanner(const Scanner& other);
    Scanner& operator==(const Scanner& other);
};

FC紧凑级

类ID;
FC紧凑级
{
    typedef std::pair<int, int> Location;

public:
    enum Direction
    {
        N, E, S, W, 
        NA = -1
    };

private:
// Scanner scanner
// Location loc
// Direction dir
// ID id
    struct    FCCImpl;
    FCCImpl  *pimpl_;

public:
    FCCompact( const ID& id,
               const Location& loc,
               const Direction& dir);

    FCCompact( const char ch,
               const Location& loc,
               const Direction& dir);

    const ID& getID()  const;
    Location  getLoc() const;
    Direction getDir() const;
    void      setDir( Direction& dir );

    void step();
    void hide();
    void show();
};

类ID

类ID
{
public:
    enum Trait
    {
        Traversable,
        NotTraversable,
        Mobile,
        Undef
    };

private:
    Trait trait_;
    char appearance_;

public:
    ID( char outputChar, Trait trait )
        : appearance_(outputChar), trait_(trait)
    { }

    ID( char outputChar )
        : appearance_(outputChar), trait_(Trait::Undef)
    { }

    char getToken()  const { return appearance_; }
    Trait getTrait() const { return trait_; }

    void setTrait(Trait trait) { trait_ = trait; }
};
最佳回答

Scanner应该有一个构造函数,该构造函数接受对World的引用;存储一个指向该论点的指针,以便以后当你需要一个世界时,你就有了一个。

不要使用单例,他们很愚蠢。你可以使用全局,但这里真的没有必要。Scanner需要一个World来操作,或者你用World构建它来操作:

struct bar;

struct foo
{
    // foo cannot operate without a bar,
    // therefore it needs to be supplied with
    // one to be able to construct
    foo(const bar& pBar) :
    mBar(&pBar)
    {}

    void i_need_bar()
    {
        mBar->use_bar();
    }

    bar* mBar;
};
问题回答

听起来您想要类<code>World</code>的singleton模式。

class World {
public:
  static World* getWorld();

private:
  static World* _singleton;
};

您可以从构造函数初始化_singleton和/或create()<如果您愿意,code>getWorld()可以返回引用(或在失败时抛出异常)。由你来思考:如果有人试图创造多个世界怎么办?是否应该在程序结束时清理World::_singleton





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签