本文介绍了引用结尾不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释为什么我在以下代码中得到对'end'的引用是不明确的"吗?我得到这是因为与std :: end发生冲突.但是,如果我将结尾放在main函数中,则不会显示该错误.在全局范围内定义与在主函数的范围内定义有何不同?

Can anyone explain why am I getting "reference to 'end' is ambiguous" is following code? I get this is because of a clash with std::end. But if I put the end inside the main function, it does not show the error. How is defining in global scope different than defining in main function's scope?

#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
     ^

推荐答案

编译器无法区分您的(全局变量) int * end std :: end 使用使用命名空间std; .避免使用 using namespace std; >.

Compiler can't differentiate between your (globals) int* end and std::end after you introduced the entire std namespace into the current (global) scope using the using namespace std;. Avoid using using namespace std;.

这篇关于引用结尾不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:23