简体   繁体   中英

C2280: “attempting to reference deleted function” for copy constructor, CC248 “'operator ='cannot access private member declared in class” (Cocos2dx)

I have a question regarding creating a 2D vector of objects during instantiation of an object.

Background: I have a class called Dungeon which needs a 2D vector of Tile objects. (Vector because I can resize since until instantiation I don't know the dimensions of the vector.)

Error:

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

UPDATE: Now I am receiving Error C2248 'cocos2d::Sprite::operator =': cannot access private member declared in class 'cocos2d::Sprite' -- Could it be a cocos2D-X issue with Sprite's operator= ? Putting Sprite declarations in public didn't help but I'm suspecting something with cocos2D-X....

My suspicion (revised): cocos2d::Sprite* floor, item, overlay, ceiling; inside my Tile class seems to cause this error to be thrown.

What I've tried: Writing my own copy constructor for Tile.


Maybe my suspicions are wrong, but if anyone could let me know why this is happening I'd be grateful!


CPP file of class (Tile)

#include "Tile.h"

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

Header file of class 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__

This time around I noticed that cocos2d classes were acting up in Visual Studio (Community 2015), so I confirmed that import "cocos2d.h" was not working. That turned out to be the problem.


So to recap, I made the following changes:

1) Made sure my IDE was including the header file properly (Visual Studio Community 2015)

2) Edited Tile.h :

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

3) Rewrote my deep copy

and then it worked.

It's a strange situation but for what it's worth, I removed my Deep Copy to see what would happen and it works without it. If it's best practice to have it, I will keep it there....

Thanks to everyone who took their time out to comment since it helped clarify a lot of mistakes that I was making.


PS If anyone thinks this question should be deleted as it is misleading please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM