Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何从类中的另一个类修改映射_C++_Class_Map - Fatal编程技术网

C++ C++;如何从类中的另一个类修改映射

C++ C++;如何从类中的另一个类修改映射,c++,class,map,C++,Class,Map,我有一个类“编辑器”和一个类“TileMap” 我希望能够使用编辑器类修改我的Tilemap类,更具体地说是Tilemap类中的std::map 更清楚地说,我不想这样做: class Editor { private: std::map <int, tilemap> m_tilemap; // <--- i want this map public: void target(std::map <int, tilemap> tilemap) //

我有一个类“编辑器”和一个类“TileMap”

我希望能够使用编辑器类修改我的Tilemap类,更具体地说是Tilemap类中的std::map

更清楚地说,我不想这样做:

class Editor
{
private:
    std::map <int, tilemap> m_tilemap; // <--- i want this map

public:
    void target(std::map <int, tilemap> tilemap) // <---- to target this map
    {
        /*  ?   */
    }

    void putABlock(sf::Vector3i coord) // <---- and modify it through several methods...
    {
       /* modify the map */
    }

};
类编辑器
{
私人:

std::map m_tilemap;//如果希望在任何情况下修改输入参数,则必须通过引用传递

void target(std::map <int, tilemap>& tilemap) // <---- to target this map
{
    ?
}

void putABlock(sf::Vector3i& coord) // <---- and modify it through several methods...
{
   /* modify the map */
}

void target(std::map&tilemap)//这里是您想要的代码:

class Editor
{
private:
    // Use a pointer to the map
    std::map<int, tilemap>* m_tilemap;

public:
    Editor()
    {
      // Set it to nullptr to avoid error in putABlock if it is called prior to
      // initialization
      m_tilemap = nullptr;
    }

    // Use reference to ensure that there is a tilemap passed. If it is optional
    // us a pointer void target(std::map<int, tilemap>* tilemap)
    void target(std::map<int, tilemap>& tilemap) 
    {
        // Assign the address of the tilemap to the pointer
        m_tilemap = &tilemap;
    }

    void putABlock(sf::Vector3i coord)
    {
        // Ensure here that we're working on something initialize.        
        if (nullptr == m_tilemap)
        {
            // Report error via one of the following method
            // 
            // * assert
            // * exeption
            // * log message

            return;
        }

       /* modify the map*/
       // Here some usage example:
       m_tilemap->insert(std::make_pair(...));
       (*m_tilemap)[...] = ...;
       (*m_tilemap)[...].tilemap_member = ...;
    }

};
如果
Tilemap
的寿命与编辑器相同,我会:

class Editor
{
private:
    // Use a instance of Tilemap 
    Tilemap m_tilemap;

public:
    Editor(/* take potential params to create the tilemap */ ) :
        m_tilemap(/* feed the tilemap with the params*/ ) 
    {      
    }

    void putABlock(sf::Vector3i coord)
    {
        // No more test needed here

       /* modify the map */
       m_tilemap.methodOnTilemap(....);
    }

};
如果每个物体的相对活跃度非常复杂,你应该看看


如果您认为确实需要直接修改
std::map
,那么我关于生动性和ref/pointer/shared_ptr的建议仍然有效。

谢谢,但我已经尝试了您的第一种方法,当我尝试修改m_tilemap时,它找不到任何成员。例如,我做了“m_tilemap[0]。block.setTexture(test);”编译器告诉我“class std::map”没有名为block的成员。@user3134405如果您想访问
m\u tilemap
中索引
0
处的
tilemap
,请尝试
m\u tilemap
中的
tilemap
。我更新了我的答案以澄清。谢谢它奏效了!我稍后会尝试第二种方法。
class Editor
{
private:
    // Use a instance of Tilemap 
    Tilemap m_tilemap;

public:
    Editor(/* take potential params to create the tilemap */ ) :
        m_tilemap(/* feed the tilemap with the params*/ ) 
    {      
    }

    void putABlock(sf::Vector3i coord)
    {
        // No more test needed here

       /* modify the map */
       m_tilemap.methodOnTilemap(....);
    }

};