星期六, 5月 09, 2015

opecv:error C2668 "cv::write" ambiguous call to overloaded function

error C2668: "cv::write" : ambiguous call to overloaded function
VC2008 之 error C2668: "cv::write" : ambiguous call to overloaded function

改 size_t 後,opencv 常送出 ambiguous call 的 error message。
乍見這類錯誤很惶恐,發生在 OpenCV 提供的 template function,
不是自己能控制的,要如何訂正呢?還好從 output 可以看出端倪。

編譯過程中的 Output window, 找出 error C2668 的上下文
..operations.hpp(2911): error C2668: 'cv::write' : ambiguous call to overloaded function
..operations.hpp(2709): could be 'void cv::write(cv::FileStorage &,const std::string &,double)'
..operations.hpp(2708): or       'void cv::write(cv::FileStorage &,const std::string &,float)'
..operations.hpp(2707): or       'void cv::write(cv::FileStorage &,const std::string &,int)'
while trying to match the argument list '(cv::FileStorage, std::string, const size_t)'
.\ex_tracer.cpp(377) : see reference to function template instantiation 'cv::FileStorage &cv::operator <<(cv::FileStorage &,const _Tp &)' being compiled
with
[
    _Tp=size_t
]

引發 error 之 source code 是下段
FileStorage fs(fname, FileStorage::WRITE);
for (size_t i = 0; i < V.size(); ++i) {
  ..
  fs << "vid" << i; // => 出現 ERROR c2668
}
.............................................................................
原來是 for_loop 中使用 size_t i, 使得 fs 困惑了. 
這類  write::(fs, name, value),
fs 只吃 double, float, int, 不接受 size_t.

size_t 轉型為 int 即可.
fs << "vid" << (int) i;  // 正確,不再出現 ERROR c2668

沒有留言: