不匹配'运营商=' in'当使用std :: map时

时间:2015-10-17 15:09:35

标签: c++

编译错误:

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

3 个答案:

答案 0 :(得分: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 ++代码中的地狱一样。有smart pointer个类或containers用于为您执行动态内存分配管理(如果实际需要)。

大部分时间你都采用RAII原则,从你的调用堆栈范围处理所有这些。

如你所说,你来自java / c#背景,其中new是获取和初始化类的任何实例的标准方法,c ++与此不同。

前面提到的语言为超出范围的实例提供自动垃圾收集,其中c ++不提供这样的机制。

因此,对于每个new,您需要应用一个平衡的delete语句,该语句容易错误处理。

好吧,如上所述,有容器类(正如你正在使用的那些)或智能指针,它们可以帮助你完成所有难以获取的东西。

答案 1 :(得分:1)

previousTagHistoryMap不是指针。你不应该指定它。

答案 2 :(得分:0)

您无法为地图对象指定其指针,为此目的在类声明中使用指针

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已经存在