簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM