【问题标题】:Method with const parameter does not accept non-const parameter?带有 const 参数的方法不接受非常量参数?
【发布时间】:2013-11-26 10:11:24
【问题描述】:

此代码无法编译:

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

但是问题是什么?我不明白。通过引用将非常量变量传递给具有 const 参数的函数,我从来没有遇到过问题。

【问题讨论】:

    标签: c++ constants parameter-passing pass-by-reference


    【解决方案1】:

    std::vector&lt;const T&gt; 不等于 std::vector&lt;T&gt; 并且不能转换为它。

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2018-08-24
      相关资源
      最近更新 更多