对结尾的引用是模棱两可的

Reference to end is ambiguous

本文关键字:模棱两可 引用 结尾      更新时间:2023-10-16

谁能解释为什么我得到"对'结束'的引用是模棱两可的"是以下代码?我明白这是因为与 std::end 的冲突。但是如果我把结尾放在主函数内,它不会显示错误。在全局范围内定义与在主函数的作用域中定义有何不同?

#include<bits/stdc++.h>
using namespace std;
int *end;
int main() {
return end == NULL;
}
prog.cpp: In function 'int main()':
prog.cpp:7:12: error: reference to 'end' is ambiguous
return end == NULL;
^
prog.cpp:4:6: note: candidates are: int* end
int *end;
^
In file included from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:94:0,
from prog.cpp:1:
/usr/include/c++/5/valarray:1226:5: note:                 template<class _Tp> const _Tp* std::end(const std::valarray<_Tp>&)
end(const valarray<_Tp>& __va)
^
/usr/include/c++/5/valarray:1216:5: note:                 template<class _Tp> _Tp* std::end(std::valarray<_Tp>&)
end(valarray<_Tp>& __va)
^
In file included from /usr/include/c++/5/string:51:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/istream:38,
from /usr/include/c++/5/sstream:38,
from /usr/include/c++/5/complex:45,
from /usr/include/c++/5/ccomplex:38,
from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:52,
from prog.cpp:1:
/usr/include/c++/5/bits/range_access.h:97:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])
end(_Tp (&__arr)[_Nm])
^
/usr/include/c++/5/bits/range_access.h:78:5: note:                 template<class _Container> decltype (__cont.end()) std::end(const _Container&)
end(const _Container& __cont) -> decltype(__cont.end())
^
/usr/include/c++/5/bits/range_access.h:68:5: note:                 template<class _Container> decltype (__cont.end()) std::end(_Container&)
end(_Container& __cont) -> decltype(__cont.end())
^
In file included from /usr/include/c++/5/bits/range_access.h:36:0,
from /usr/include/c++/5/string:51,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/istream:38,
from /usr/include/c++/5/sstream:38,
from /usr/include/c++/5/complex:45,
from /usr/include/c++/5/ccomplex:38,
from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:52,
from prog.cpp:1:
/usr/include/c++/5/initializer_list:99:5: note:                 template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)
end(initializer_list<_Tp> __ils) noexcept
^

编译器在使用using namespace std;将整个std命名空间引入当前(全局(作用域后,无法区分(全局(int* end和 std::end。避免使用using namespace std;.