0

I am working on a project involving sockets and QT. I want to use the socket functions from within sys/socket.h and not the ones that come with QT. (this is because I am following some tutorial type stuff).

The following code:

if (connect(sock, (const struct sockaddr *) &servAddr, (socklen_t) sizeof(servAddr)) < 0){    //connect to server

caused the following error:

error: no matching function for call to 'MainWindow::connect(int&, const sockaddr*, socklen_t)'

I fixed this by adding :: in front of connect() like so:

if (::connect(sock, (const struct sockaddr *) &servAddr, (socklen_t) sizeof(servAddr)) < 0){    //connect to server

As I understand it I can use :: prefixed with a namespace but what does it mean in the current use? I only found out how to fix my error from a forum post but it did not explain the underlying thought behind it. Any other tricks for using :: .

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
NDEthos
  • 507
  • 6
  • 17
  • 1
    The _global_ namespace ... – πάντα ῥεῖ Jun 09 '14 at 22:20
  • Explaining your problem, names are looked up by scope before overload resolution happens; so since you have an entity `MainWindow::connect`, if you write `connect` within MainWindow code then it only finds `MainWindow::connect`. When you write `::connect` it means to use the global namespace's `connect`. – M.M Jun 09 '14 at 23:36

1 Answers1

1

It means to take the expression after it from global scope. See this answer for more details.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329