[英]no match for 'operator=' in ' when using std::map
编译错误:
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>)))'
如果我正确理解这一点,则说明我不能使用赋值运算符。 这是为什么?
标题代码:
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代码:
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
}
整个编译结果:
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
只需摆脱此代码行
previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >;
您的成员声明中已经存在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.
};
在一般情况下,避免使用new
,例如c ++代码中的地狱。 有智能指针类或用于为您执行动态内存分配管理的容器 (如果实际需要)。
在大多数情况下,您只是遵循RAII原则,该原则从您的调用堆栈范围处理所有这些问题。
正如您提到的,您来自Java / c#背景,其中new
是获取和初始化类的任何实例的标准方法,而c ++与此不同。
前面提到的语言为超出范围的实例提供自动垃圾收集,而c ++不提供这种机制。
因此,对于每个new
,您都需要应用平衡的delete
语句,该语句易于正确处理。
好了,正如前面提到的,有一些容器类(如您正在使用的容器类)或智能指针,它们为您提供了所有难以获得的东西。
previousTagHistoryMap
不是指针。 您不应为其分配指针。
您不能将其指针分配给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
比它会正常工作
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
}
不要忘记复制构造函数,赋值运算符和析构函数,以进行适当的内存管理
或根本不使用
previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >; //complains about this line
相反,默认情况下已经创建了previousTagHistoryMap
而且你不应该那样做
previousTagHistoryMap = new std::map< uint32_t, std::list<Addr_t> >;
因为previousTagHistoryMap
已经存在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.