I got a server class in server.h:
namespace tcp{
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
class Server{
private:
int listener; //socket fd
public:
/* ...... */
void run();
int listen();
/* ...... */
}
}
In server.cpp:
usinge namespace tcp;
void Server::run(){
/* ...do something to get socket for listening...*/
listen();
/* ...do something else...*/
}
int Server::listen(){
if (listen(listener, 10) == -1){
perror("listen");
exit(3)
}
}
The compiler can’t distinguish between Server::listen() call and the listen() system call. My solution is to wrap the #include directives inside the namespace std. However, I’m not sure if it will conflict if other files include those system headers, or some other problems. Any suggestion other than name changing?
Prefix the C version with
::i.e.
i.e. using the global namespace.