0

我做了一个unix域服务器客户端。它可以完美运行,没有任何错误。

然后我想使用头文件中的random_shuffle()函数。algorithm所以我只包含algorithm头文件。我没有进行任何其他更改。但是现在在编译时出现以下错误:

game.cpp:102:85: error: no match for ‘operator!=’ in ‘std::bind(_Functor&&, _ArgTypes&& ...) [with _Functor = int&, _ArgTypes = {sockaddr*, long unsigned int}, typename std::_Bind_helper<_Functor, _ArgTypes>::type = std::_Bind<int(sockaddr*, long unsigned int)>]((* &((sockaddr*)(& address))), (* &110ul)) != 0’
game.cpp:102:85: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_iterator.h:1037:5: note: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_iterator.h:1043:5: note: template<class _Iterator> bool std::operator!=(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/postypes.h:223:5: note: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/allocator.h:132:5: note: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:137:5: note: template<class _Tp> bool std::operator!=(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2473:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2485:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2497:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:200:5: note: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/include/c++/4.6/bits/stl_vector.h:1297:5: note: template<class _Tp, class _Alloc> bool std::operator!=(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/random.h:338:5: note: template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> bool std::operator!=(const std::linear_congruential_engine<_UIntType, __a, __c, __m>&, const std::linear_congruential_engine<_UIntType, __a, __c, __m>&)
/usr/include/c++/4.6/bits/random.h:568:5: note: template<class _UIntType, long unsigned int __w, long unsigned int __n, long unsigned int __m, long unsigned int __r, _UIntType __a, long unsigned int __u, _UIntType __d, long unsigned int __s, _UIntType __b, long unsigned int __t, _UIntType __c, long unsigned int __l, _UIntType __f> bool std::operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>&, const std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>&)
/usr/include/c++/4.6/bits/random.h:765:5: note: template<class _UIntType, long unsigned int __w, long unsigned int __s, long unsigned int __r> bool std::operator!=(const std::subtract_with_carry_engine<_UIntType, __w, __s, __r>&, const std::subtract_with_carry_engine<_UIntType, __w, __s, __r>&)
/usr/include/c++/4.6/bits/random.h:987:5: note: template<class _RandomNumberEngine, long unsigned int __p, long unsigned int __r> bool std::operator!=(const std::discard_block_engine<_RandomNumberEngine, __p, __r>&, const std::discard_block_engine<_RandomNumberEngine, __p, __r>&)
/usr/include/c++/4.6/bits/random.h:1183:5: note: template<class _RandomNumberEngine, long unsigned int __w, class _UIntType> bool std::operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w, _UIntType>&, const std::independent_bits_engine<_RandomNumberEngine, __w, _UIntType>&)
/usr/include/c++/4.6/bits/random.h:1433:5: note: template<class _RandomNumberEngine, long unsigned int __k> bool std::operator!=(const std::shuffle_order_engine<_RandomNumberEngine, __k>&, const std::shuffle_order_engine<_RandomNumberEngine, __k>&)
/usr/include/c++/4.6/bits/random.h:1735:5: note: template<class _IntType> bool std::operator!=(const std::uniform_int_distribution<_IntType>&, const std::uniform_int_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:1916:5: note: template<class _IntType> bool std::operator!=(const std::uniform_real_distribution<_IntType>&, const std::uniform_real_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:2136:5: note: template<class _RealType> bool std::operator!=(const std::normal_distribution<_RealType>&, const std::normal_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:2312:5: note: template<class _RealType> bool std::operator!=(const std::lognormal_distribution<_RealType>&, const std::lognormal_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:2504:6: note: template<class _RealType> bool std::operator!=(const std::gamma_distribution<_RealType>&, const std::gamma_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:2669:5: note: template<class _RealType> bool std::operator!=(const std::chi_squared_distribution<_RealType>&, const std::chi_squared_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:2808:5: note: template<class _RealType> bool std::operator!=(const std::cauchy_distribution<_RealType>&, const std::cauchy_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:3024:5: note: template<class _RealType> bool std::operator!=(const std::fisher_f_distribution<_RealType>&, const std::fisher_f_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:3198:5: note: template<class _RealType> bool std::operator!=(const std::student_t_distribution<_RealType>&, const std::student_t_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:3346:3: note: bool std::operator!=(const std::bernoulli_distribution&, const std::bernoulli_distribution&)
/usr/include/c++/4.6/bits/random.h:3346:3: note:   no known conversion for argument 1 from ‘std::_Bind_helper<int&, sockaddr*, long unsigned int>::type {aka std::_Bind<int(sockaddr*, long unsigned int)>}’ to ‘const std::bernoulli_distribution&’
/usr/include/c++/4.6/bits/random.h:3583:5: note: template<class _IntType> bool std::operator!=(const std::binomial_distribution<_IntType>&, const std::binomial_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:3725:5: note: template<class _IntType> bool std::operator!=(const std::geometric_distribution<_IntType>&, const std::geometric_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:3931:5: note: template<class _IntType> bool std::operator!=(const std::negative_binomial_distribution<_IntType>&, const std::negative_binomial_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:4119:5: note: template<class _IntType> bool std::operator!=(const std::poisson_distribution<_IntType>&, const std::poisson_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:4269:5: note: template<class _RealType> bool std::operator!=(const std::exponential_distribution<_RealType>&, const std::exponential_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:4444:5: note: template<class _RealType> bool std::operator!=(const std::weibull_distribution<_RealType>&, const std::weibull_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:4619:5: note: template<class _RealType> bool std::operator!=(const std::extreme_value_distribution<_RealType>&, const std::extreme_value_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:4851:5: note: template<class _IntType> bool std::operator!=(const std::discrete_distribution<_IntType>&, const std::discrete_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.h:5090:5: note: template<class _RealType> bool std::operator!=(const std::piecewise_constant_distribution<_RealType>&, const std::piecewise_constant_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:5332:5: note: template<class _RealType> bool std::operator!=(const std::piecewise_linear_distribution<_RealType>&, const std::piecewise_linear_distribution<_RealType>&)
/usr/include/c++/4.6/tuple:597:5: note: template<class ... _TElements, class ... _UElements> bool std::operator!=(const std::tuple<_TElements ...>&, const std::tuple<_Elements ...>&)
/usr/include/c++/4.6/functional:2245:5: note: template<class _Res, class ... _Args> bool std::operator!=(const std::function<_Res(_ArgTypes ...)>&, std::nullptr_t)
/usr/include/c++/4.6/functional:2251:5: note: template<class _Res, class ... _Args> bool std::operator!=(std::nullptr_t, const std::function<_Res(_ArgTypes ...)>&)
/usr/include/c++/4.6/bits/unordered_map.h:210:5: note: template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc, bool __cache_hash_code> bool std::operator!=(const std::__unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&, const std::__unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&)
/usr/include/c++/4.6/bits/unordered_map.h:228:5: note: template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc, bool __cache_hash_code> bool std::operator!=(const std::__unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&, const std::__unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&)
/usr/include/c++/4.6/bits/unordered_map.h:390:5: note: template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc> bool std::operator!=(const std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&, const std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&)
/usr/include/c++/4.6/bits/unordered_map.h:402:5: note: template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc> bool std::operator!=(const std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>&, const std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>&)

line 102正在使用bind之前正常工作的功能。这是line 102

    if(bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0){

我没有做任何其他事情,我只是包含了这个文件,那么为什么我会收到这个错误?

如果它可以帮助我使用以下代码行编译它:

g++ -std=c++0x -I /usr/include/postgresql -lpq -fpermissive -c game.cpp
4

1 回答 1

7

这是一个典型的名称冲突案例,原因是不加选择地使用using在全局命名空间中导入的指令和 C API。

您打算调用的bind是用于套接字的全局函数(来自<sys/sockets.h>),但编译器更喜欢std::bind提供的 in <algorithm>,您可能使用臭名昭著的using namespace std;.

为了消除歧义,告诉编译器使用全局绑定而不是来自以下的绑定std

if(::bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0){

::前面没有任何命名空间的运算符表示“从全局命名空间中获取它”,除非原始标头包装在一个namespace块中,否则每个 C API 都将被放入其中)

无论如何,删除using namespace std;指令可能是更好的选择,更喜欢在需要时使用或仅使用您实际经常使用的东西std::进行导入。using

于 2012-12-28T15:50:18.660 回答