Ollie
Ollie

Reputation: 463

socket cannot be used as a function

Hi I have done Java and C# and recently did a module in C/C++ on the Linux environment. I have decided to make a simple UDP server in C++ in windows which I am new too but not a complete newbie when it comes to coding. I have looked as some examples and it all seems to work apart from this one line:

SOCKET socket = socket(AF_INET, SOCK_DGRAM, 0);

when I compile this in Netbeans it comes up with this error message:

classUDPServer.cpp: In constructor 'UDPServer::UDPServer()':
classUDPServer.cpp:35: error: '((UDPServer*)this)->UDPServer::socket' cannot be used as a function
    make[2]: *** [build/Debug/MinGW-Windows/classUDPServer.o] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2

I have both winsock2.h and iostream included.

Can anyone help me with this error?

Thanks in advance!

Upvotes: 0

Views: 2041

Answers (1)

mfontanini
mfontanini

Reputation: 21900

Don't name your variable the same way as a function.

SOCKET my_socket = socket(AF_INET, SOCK_DGRAM, 0);

Moreover, as @chris points out, you could use the scope resolution operator(::) so that the function socket is looked up in the global namespace:

SOCKET socket = ::socket(AF_INET, SOCK_DGRAM, 0);

Upvotes: 7

Related Questions