3

このエラー:

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)  c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility  216

この関数の唯一の行で発生します。

void Animation::AddAnimation(std::string name, AnimationFrameSet& animation) {
    _animations.insert(std::make_pair(name, animation));
}

_animationsですstd::map<std::string, AnimationFrameSet>

AnimationFrameSet は operator=(...) とコピー コンストラクターを宣言しますが、奇妙なことにconst std::string、文字列がconst.

私は一生、これがなぜエラーをスローする/スローする必要があるのか​​\u200b\u200b理解することはできません(または覚えています! :P)。

ありがとう。

編集

これが機能しない理由について少し混乱している理由は、別のクラスが非常によく似た実装を使用しており、エラーがスローされないためです。

BITMAP* BitmapCache::GetBitmap(std::string filename) {
    //Return NULL if a bad filename was passed.
    if(filename.empty()) return NULL;
    if(exists(filename.c_str()) == false) return NULL;

    //Reduce incorrect results by forcing slash equality.
    filename = fix_filename_slashes(&filename[0]);

    //Clean the cache if it's dirty.
    CleanCache();

    //Search for requested BITMAP.
    MapStrBmpIter _iter = _cache.find(filename);

    //If found, return it.
    if(_iter != _cache.end()) return _iter->second;

    //Otherwise, create it, store it, then return it.
    BITMAP* result = load_bmp(filename.c_str(), NULL);
    if(result == NULL) return NULL;
    /*Similar insert line, a non-const std::string that was passed in is passed to a std::make_pair(...) function*/
    _cache.insert(std::make_pair(filename, result));
    return result;
}

typedef:

typedef std::map<std::string, BITMAP*> MapStrBmp;
typedef MapStrBmp::iterator MapStrBmpIter;
4

2 に答える 2

0

_animationsは であるためstd::map、次のような別の挿入方法を使用してみてください。

_animations[name] = animation;

しかし、もっと重要なことは、AnimationFrameSet クラスに有効なコピー コンストラクターと代入演算子があるかどうかです。そうでない場合は、次のようなスマート ポインター クラスを使用できます。

typedef std::shared_ptr<AnimationFrameSet> AnimationFrameSetPtr;

次に、マップは次のようになります。

std::map<std::string, AnimationFrameSetPtr>
于 2011-10-15T07:14:33.333 に答える
0

これが失敗する理由は、使用している std::map コンテナーが const 値をキー値として想定しているためです。

std::map hereのドキュメントを確認してください。

例:

class TestClass
{
};

std::map< const std::string, TestClass > myMap;
std::pair< const std::string, TestClass > firstElement = std::make_pair("test", TestClass() );
myMap.insert( firstElement );
于 2011-10-15T08:48:49.573 に答える