不明确的C ++编译器错误 [英] Ambiguous C++ compiler error

查看:259
本文介绍了不明确的C ++编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下位代码无法编译。错误似乎是对合并例程的某种模糊调用。我的理解是STL有一个合并例程,在std命名空间中找到,但就我可以告诉名称合并在下面的代码应该是唯一的。



如果我将合并重命名为xmerge,一切正常。问题是什么?哪里是来自的名称冲突?



http://codepad.org/uAKciGy5

  #include< iostream> 
#include #include< vector>

template< typename InputIterator1,
typename InputIterator2,
typename OutputIterator>
void merge(const InputIterator1 begin1,const InputIterator1 end1,
const InputIterator2 begin2,const InputIterator2 end2,
OutputIterator out)
{
InputIterator1 itr1 = begin1;
InputIterator2 itr2 = begin2;
while((itr1!= end1)&&(itr2!= end2))
{
if(* itr1 <* itr2)
* out = * itr1 ,++ itr1;
else
* out = * itr2,++ itr2;
++ out;
}
while(itr1!= end1)* out ++ = * itr1 ++;
while(itr2!= end2)* out ++ = * itr2 ++;
}

int main()
{
std :: vector< int> l1;
std :: vector< int> l2;
std :: vector< int> merged_list;

merge(l1.begin(),l1.end(),
l2.begin(),l2.end(),
std :: back_inserter(merged_list)) ;

return 0;
}


解决方案

编译器合并函数和算法中定义的 std :: merge >。使用 :: merge 可删除此模糊。此调用不明确,因为编译器使用参数依赖查找在使用不合格函数名称时搜索函数。 / p>

The following bit of code fails to compile. The error seems to be some kind of ambigous call to the merge routine. My understanding is that STL has a merge routine found in the std namespace, but as far as I can tell the name merge in the code below should be unique.

If I rename merge to xmerge, everything works. What could the problem be? where is the name clash coming from?

http://codepad.org/uAKciGy5

#include <iostream>
#include <iterator>
#include <vector>

template<typename InputIterator1,
         typename InputIterator2,
         typename OutputIterator>
void merge(const InputIterator1 begin1, const InputIterator1 end1,
           const InputIterator2 begin2, const InputIterator2 end2,
           OutputIterator out)
{
   InputIterator1 itr1 = begin1;
   InputIterator2 itr2 = begin2;
   while ((itr1 != end1) && (itr2 != end2))
   {
      if (*itr1 < *itr2)
         *out = *itr1, ++itr1;
      else
         *out = *itr2, ++itr2;
      ++out;
   }
   while (itr1 != end1) *out++ = *itr1++;
   while (itr2 != end2) *out++ = *itr2++;
}

int main()
{
   std::vector<int> l1;
   std::vector<int> l2;
   std::vector<int> merged_list;

   merge(l1.begin(),l1.end(),
         l2.begin(),l2.end(),
         std::back_inserter(merged_list));

   return 0;
}

解决方案

Compiler is getting confused between your merge function and the std::merge defined in the algorithm. Use ::merge to remove this ambiguity. This call is ambiguous as compiler is using Argument Dependendent Lookup to search for the function when unqualified function name is used.

这篇关于不明确的C ++编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆