Home > Back-end >  error: use of undeclared identifier 'localhost'
error: use of undeclared identifier 'localhost'

Time:10-06

I am implementing a TCP Client application. I am trying following line of code:

socket->connectToHost (localhost, 1234);

I am getting the error shown below:

error: use of undeclared identifier 'localhost'

I understand it doesn't have the correct header included. Which header contains "localhost"?

CodePudding user response:

localhost is a variable name ("identifier"). You probably meant connectToHost("localhost", 1234);.

And while drilling further into the docs, it seems that SpecialAddress::LocalHost is a thing, too, defined next to the socket class. So something like this should also work:

auto localhost = QHostAddress(QSpecialAddress::LocalHost);
socket->connectToHost(localhost, 1234);
  • Related