简体   繁体   中英

no match for 'operator=' in ' when using std::map

Compile error:

build/SMP_BOOKSIM/libsuc/CacheCore.cpp:243:2: error: no match for 'operator=' in '((CacheAssoc<SMPCacheState, unsigned int, false>*)this)->CacheAssoc<SMPCacheState, unsigned int, false>::previousTagHistoryMap = (operator new(24u), (<statement>, ((std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >*)<anonymous>)))'

If I'm understanding this correctly, it says I can't use the assignment operator. Why is that?

Header code:

template<class State, class Addr_t = uint32_t, bool Energy=false>
class CacheAssoc : public CacheGeneric<State, Addr_t, Energy> {
    //using XYZ classes
protected:
    std::map<uint32_t, std::list<Addr_t>> previousTagHistoryMap;
    //more stuff below
};

CPP code:

template<class State, class Addr_t, bool Energy>
CacheAssoc<State, Addr_t, Energy>::CacheAssoc(int32_t size, int32_t assoc, int32_t blksize, int32_t addrUnit, const char *pStr)
    : CacheGeneric<State, Addr_t, Energy>(size, assoc, blksize, addrUnit)
{
    //code
    previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >; //complains about this line
}

Entire compile results:

scons build/SMP_BOOKSIM/sesc.opt
scons: Reading SConscript files ...
SESC Simulator
Reading SConsopts
Building in /home/ud233user/sesc/build/SMP_BOOKSIM
Using variable in /home/ud233user/sesc/build_opts/SMP_BOOKSIM
scons: done reading SConscript files.
scons: Building targets ...
 [     CXX] SMP_BOOKSIM/libcmp/cmp.cpp -> .o
 [     CXX] SMP_BOOKSIM/libcmp/SMPCache.cpp -> .o
In file included from build/SMP_BOOKSIM/libsuc/CacheCore.h:424:0,
                 from build/SMP_BOOKSIM/libcore/GMemoryOS.h:32,
                 from build/SMP_BOOKSIM/libcore/GMemorySystem.h:28,
                 from build/SMP_BOOKSIM/libmem/MemorySystem.h:30,
                 from build/SMP_BOOKSIM/libcmp/SMemorySystem.h:26,
                 from build/SMP_BOOKSIM/libcmp/SMPCache.h:26,
                 from build/SMP_BOOKSIM/libcmp/SMPCache.cpp:22:
build/SMP_BOOKSIM/libsuc/CacheCore.cpp: In constructor 'CacheAssoc<State, Addr_t, Energy>::CacheAssoc(int32_t, int32_t, int32_t, int32_t, const char*) [with State = SMPCacheState, Addr_t = unsigned int, bool Energy = false, int32_t = int]':
build/SMP_BOOKSIM/libsuc/CacheCore.cpp:197:9:   instantiated from 'static CacheGeneric<State, Addr_t, Energy>* CacheGeneric<State, Addr_t, Energy>::create(const char*, const char*, const char*, ...) [with State = SMPCacheState, Addr_t = unsigned int, bool Energy = false]'
build/SMP_BOOKSIM/libcmp/SMPCache.cpp:136:48:   instantiated from here
build/SMP_BOOKSIM/libsuc/CacheCore.cpp:243:2: error: no match for 'operator=' in '((CacheAssoc<SMPCacheState, unsigned int, false>*)this)->CacheAssoc<SMPCacheState, unsigned int, false>::previousTagHistoryMap = (operator new(24u), (<statement>, ((std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >*)<anonymous>)))'
build/SMP_BOOKSIM/libsuc/CacheCore.cpp:243:2: note: candidates are:
/usr/include/c++/4.6/bits/stl_map.h:253:7: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = unsigned int, _Tp = std::list<unsigned int, std::allocator<unsigned int> >, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > >, std::map<_Key, _Tp, _Compare, _Alloc> = std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >]
/usr/include/c++/4.6/bits/stl_map.h:253:7: note:   no known conversion for argument 1 from 'std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >*' to 'const std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >&'
/usr/include/c++/4.6/bits/stl_map.h:268:7: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = unsigned int, _Tp = std::list<unsigned int, std::allocator<unsigned int> >, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > >, std::map<_Key, _Tp, _Compare, _Alloc> = std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >]
/usr/include/c++/4.6/bits/stl_map.h:268:7: note:   no known conversion for argument 1 from 'std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >*' to 'std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >&&'
/usr/include/c++/4.6/bits/stl_map.h:289:7: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::initializer_list<std::pair<const _Key, _Tp> >) [with _Key = unsigned int, _Tp = std::list<unsigned int, std::allocator<unsigned int> >, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > >, std::map<_Key, _Tp, _Compare, _Alloc> = std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >]
/usr/include/c++/4.6/bits/stl_map.h:289:7: note:   no known conversion for argument 1 from 'std::map<unsigned int, std::list<unsigned int, std::allocator<unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > > >*' to 'std::initializer_list<std::pair<const unsigned int, std::list<unsigned int, std::allocator<unsigned int> > > >'
scons: *** [build/SMP_BOOKSIM/libcmp/SMPCache.o] Error 1
scons: building terminated because of errors.
make: *** [sesc.opt] Error 2

Just get rid of this code line

previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >;

The instance of previousTagHistoryMap is already present from your member declaration:

template<class State, class Addr_t = uint32_t, bool Energy=false>
class CacheAssoc : public CacheGeneric<State, Addr_t, Energy> {
     //using XYZ classes
protected:
     std::map<uint32_t, std::list<Addr_t>> previousTagHistoryMap;
  // ^^^^^^^^^^^^ An instance is automatically created on construction.
};

In general usage of new is avoided like hell in c++ code. There are either smart pointer classes, or containers that are used to do the dynamic memory allocation management for you (if actually needed).

Most of the time you just go with the RAII principle, that handles all of this from your call stack scope.


As you mentioned you're coming here from a java/c# background, where new is the standard way to obtain and initialize any instances of a class, c++ is different regarding this.

The former mentioned languages provide automatic garbage collection for instances going out of scope, where c++ doesn't provide such mechanism.

So for every new , you need to apply a balanced delete statement, which is error prone to handle correctly.

Well, as mentioned there are the container classes (as those you're using) or smart pointers, that do all of the hard to get stuff for you.

previousTagHistoryMap is not a pointer. You should not assign a pointer to it.

You can not assign to map object its pointer, use pointer in class declaration for this purpose

template<class State, class Addr_t = uint32_t, bool Energy=false>
class CacheAssoc : public CacheGeneric<State, Addr_t, Energy> {
    //using XYZ classes
protected:
    std::map<uint32_t, std::list<Addr_t>>* previousTagHistoryMap;
    //more stuff below

than it will work correctly

template<class State, class Addr_t, bool Energy>
CacheAssoc<State, Addr_t, Energy>::CacheAssoc(int32_t size, int32_t assoc, int32_t blksize, int32_t addrUnit, const char *pStr)
    : CacheGeneric<State, Addr_t, Energy>(size, assoc, blksize, addrUnit)
{
    //code
    previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >; //complains about this line
}

do not forget about copy constructor, assignment operator and destructor, for proper memory management

or simply do not use

previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >; //complains about this line

instead previousTagHistoryMap already was created by default

and you do not suppose to do that

previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >;

because previousTagHistoryMap already exists

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