c++ - 不明确的 C++ 编译器错误

标签 c++ namespaces compiler-errors ambiguous

以下代码无法编译。该错误似乎是对合并例程的某种模棱两可的调用。我的理解是,STL 在 std 命名空间中有一个合并例程,但据我所知,下面代码中的名称 merge 应该是唯一的。

如果我将 merge 重命名为 xmerge,一切正常。可能是什么问题?名称冲突从何而来?

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;
}

最佳答案

编译器混淆了您的 merge 函数和 algorithm 中定义的 std::merge。使用 ::merge 消除这种歧义。此调用不明确,因为编译器正在使用 Argument Dependendent Lookup当使用不合格的函数名称时搜索函数。

关于c++ - 不明确的 C++ 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4873106/

相关文章:

c++ - 如何公开库的枚举,以便我的代码不必键入整个命名空间来使用该枚举?

C++ 运算符重载和关联的命名空间

python - 一次性导入所有子模块

javascript - 元素类型无效: expected a string (for built-in components) … Check the render method of `App`

c++ - 应用中的脚本系统

c++ - C++11 委托(delegate)构造函数功能的实现导致了几个警告

c++ - 已在 ConsoleApplication1.obj 中定义

c++ - 使用RTTI克隆唯一指针的 vector

java - 如何将 xml 命名空间添加到 Java 中的非根元素?

gcc - 如何在 Beaglebone Black 上编译 openssl 1.0.2f?