C2280:复制构造函数的“尝试引用引用的已删除函数”,CC248“'operator ='无法访问在类中声明的私有成员”(Cocos2dx)

维吉尔9306

我有一个关于在对象实例化期间创建对象的2D向量的问题。

背景:我有一个名为Dungeon的类,该类需要Tile对象的2D向量。(矢量,因为我可以调整大小,因为直到实例化之前我都不知道矢量的尺寸。)

错误:

Error   C2280   'Tile &Tile::operator =(const Tile &)': attempting to reference a deleted function  MyCppGame   c:\users\dante\git\yshacpp\mycppgame\classes\tile.cpp   18  

更新:现在我收到了Error C2248 'cocos2d::Sprite::operator =': cannot access private member declared in class 'cocos2d::Sprite'—这可能是Sprite的cocos2D-X问题operator=吗?放入Sprite声明public没有帮助,但我怀疑与cocos2D-X有关。

我的怀疑(修订): cocos2d::Sprite* floor, item, overlay, ceiling;在我的Tile课堂内部似乎导致抛出此错误。

我尝试过的事情:为Tile编写自己的副本构造函数。


也许我的猜想是错的,但是如果有人可以让我知道为什么会这样,我将不胜感激!


类别的CPP文件(平铺)

#include "Tile.h"

Tile::Tile()  : block(false), hasCharacter(false) { /* Nothing */ }

Tile的头文件

#ifndef __TILE_H__
#define __TILE_H__

#include ...

class Tile {
protected:
    bool deepCopy(const Tile& copyTile) {
        bool result = false;
        if (&copyTile != this) {
            this->character = copyTile.character;
            this->floor = copyTile.floor;
            this->item = copyTile.item;
            this->overlay = copyTile.overlay;
            this->ceiling = copyTile.ceiling;
            result = true;
        }
        return result;
    }

    cocos2d::Sprite* floor, item, overlay, ceiling;

// private: // nothing atm
public:
    bool block, hasCharacter;
    Character character;

    Tile();
    Tile(const Tile& copyTile) { deepCopy(copyTile); };
    Tile& operator=(const Tile& copyTile) { deepCopy(copyTile); return *this; }
    //~Tile();
};

#endif // __TILE_H__
维吉尔9306

这次,我注意到cocos2d类正在Visual Studio中工作(社区2015),因此我确认这import "cocos2d.h"是行不通的。原来是问题所在。


综上所述,我进行了以下更改:

1)确保我的IDE正确包含了头文件(Visual Studio Community 2015)

2)编辑Tile.h

class Tile { ...
private:
    cocos2d::Sprite* floor;
    cocos2d::Sprite* item;
    cocos2d::Sprite* overlay;
    cocos2d::Sprite* ceiling
...
}

3)重写了我的深拷贝

然后它起作用了。

这是一个奇怪的情况,但是出于其价值,我删除了Deep Deep以查看会发生什么,如果没有它,它会起作用。如果是最佳做法,我会保留在那里。

感谢所有抽出宝贵时间发表评论的人,因为它有助于弄清我犯的很多错误。


附言:如果有人认为此问题应被误导删除,请告诉我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章