簡體   English   中英

具有const參數的方法不接受非const參數嗎?

[英]Method with const parameter does not accept non-const parameter?

此代碼無法編譯:

ErrorTolerantSearch e;
e.readStringsFromFile("test.txt");
e.buildQgramIndex(3);
vector<map<uint, uint>*> lists;
lists.push_back(&e._qgramIndex["ret"]); // ignore this, assume container not empty
lists.push_back(&e._qgramIndex["coo"]); // ignore this, assume container not empty
map<uint, uint> resunion = e.computeUnion(lists); // <-- this makes problems

這是標題的一部分

class ErrorTolerantSearch {
 public:
  void readStringsFromFile(string fileName);
  void buildQgramIndex(uint  q);
  map<uint, uint> computeUnion(const vector<const map<uint, uint>*> & lists);
  map<string, map<uint, uint> > _qgramIndex;
};

這是編譯器給出的錯誤:

ErrorTolerantSearchTest.cpp: In member function ‘virtual void ErrorTolerantSearchTest_computeUnion_Test::TestBody()’:
ErrorTolerantSearchTest.cpp:89:50: error: no matching function for call to ‘ErrorTolerantSearch::computeUnion(std::vector<std::map<unsigned int, unsigned int>*>&)’
ErrorTolerantSearchTest.cpp:89:50: note: candidate is:
In file included from ErrorTolerantSearchTest.cpp:36:0:
./ErrorTolerantSearch.h:56:19: note: std::map<unsigned int, unsigned int> ErrorTolerantSearch::computeUnion(const std::vector<const std::map<unsigned int, unsigned int>*>&)
./ErrorTolerantSearch.h:56:19: note:   no known conversion for argument 1 from ‘std::vector<std::map<unsigned int, unsigned int>*>’ to ‘const std::vector<const std::map<unsigned int, unsigned int>*>&’
make[1]: *** [ErrorTolerantSearchTest] Fehler 1

但是有什么問題呢? 我不明白。 通過引用將非常量變量傳遞給具有常量參數的函數,我從來沒有遇到過問題。

std::vector<const T>不等於std::vector<T>並且不能轉換為它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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