1

I have the following class:

class AssetManager
{
public:
    AssetManager();
    AssetManager(std::vector<const char *> sources);
    ~AssetManager();
    bool addSource(const char *file);
    std::vector<char> getAsset(const char *name);

private:
    class Asset
    {
    public:
        Asset(const char *name, std::size_t size, std::size_t location, std::ifstream &inputStream);
        ~Asset();
        const char *getName();
        std::size_t getSize();
        std::size_t getLocation();
        std::ifstream &getInputStream();

        //makes the name 16 bytes and filled with 0's
        static const char *makeName(const char *name);

    private:
        char name_[ASSET_NAME_LENGTH];
        std::size_t size_;
        std::size_t location_;
        std::ifstream &inputStream_;
    };
    Asset *findAssetByName(std::string &name, std::size_t start, std::size_t end);
    std::vector<std::pair<std::string, Asset>> sortedNames_;
    std::vector<std::ifstream> inputStreams_;
};

The portion of code that causes the problem:

AssetManager::AssetManager(std::vector<const char*> sources) : inputStreams_(sources.size())
{
    //some code....

    std::sort(sortedNames_.begin(), sortedNames_.end(),
        [](std::pair<std::string, Asset> const& a,
            std::pair<std::string, Asset> const& b){return a.first.compare(b.first) < 0;});
}

I get the following error when trying to compile

Severity    Code    Description Project File    Line
Error   C2280   'AssetManager::Asset &AssetManager::Asset::operator =(const AssetManager::Asset &)': attempting to reference a deleted function c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility  175

I know the problem is the parameter, but I don't understand why. If const std::pair<std::string, Asset> const& a is a reference to a pair of string and Asset, why is the assignment operator called?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Douglas
  • 183
  • 1
  • 7
  • 1
    Possible duplicate of [Rule-of-Three becomes Rule-of-Five with C++11?](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11) – Pradhan Nov 25 '15 at 00:35

1 Answers1

1

The assignment operator is called by sort to swap two elements that are out of order. It is used in your case because there is not a swap(Asset &, Asset &) function defined, so the default swap is used which will copy thru a temporary.

In technical terms, sort requires it's arguments to be ValueSwappable, which a class with a reference member will not be.

You need to provide a swap or assignment operator for sort to work.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56