简体   繁体   English

使用std :: map时,'中的'operator ='不匹配

[英]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: CPP代码:

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: 您的成员声明中已经存在previousTagHistoryMap的实例:

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. 在一般情况下,避免使用new ,例如c ++代码中的地狱。 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. 在大多数情况下,您只是遵循RAII原则,该原则从您的调用堆栈范围处理所有这些问题。


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. 正如您提到的,您来自Java / c#背景,其中new是获取和初始化类的任何实例的标准方法,而c ++与此不同。

The former mentioned languages provide automatic garbage collection for instances going out of scope, where c++ doesn't provide such mechanism. 前面提到的语言为超出范围的实例提供自动垃圾收集,而c ++不提供这种机制。

So for every new , you need to apply a balanced delete statement, which is error prone to handle correctly. 因此,对于每个new ,您都需要应用平衡的delete语句,该语句易于正确处理。

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. previousTagHistoryMap不是指针。 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 您不能将其指针分配给map对象,为此目的请在类声明中使用指针

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 相反,默认情况下已经创建了previousTagHistoryMap

and you do not suppose to do that 而且你不应该那样做

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

because previousTagHistoryMap already exists 因为previousTagHistoryMap已经存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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