SOCKET variable remains undeclared after being declared in another function

209 views Asked by At

I have a quite Naive problem about the SOCKET variable, i really have no idea how it works.

I am coding one class used to receive data from a TCPIP connect. I defined a variable of SOCKET in this class called m_ipsocket, and declared it in one function InitializedConnection(), but when i use this variable in another function (startreceiving()), it says this variable is an undeclared identifier.

How should i make change? I donot think it is something vary hard but for sure it is something i donot know. Thanks.

(this is how I define the variable in the class)

// variable for TCPIP connection
SOCKADDR_IN m_addr;
WSADATA m_wsadata;
SOCKET m_ipsocket;

(this is the function I used to declare the operation)

bool GazeTracking::InitializeConnection()
{
// build the connection with the eye tracker

if(WSAStartup(0x0101, &m_wsadata))
{   
    return 0;
}

m_ipsocket = socket(AF_INET, SOCK_STREAM, 0);
if(m_ipsocket == INVALID_SOCKET)
{
    return 0;
}
u_long poll = TRUE;
if(ioctlsocket(m_ipsocket, FIONBIO, &poll) == SOCKET_ERROR)
{
    return 0;
}

m_addr.sin_family = AF_INET;
m_addr.sin_port = htons(4242);
m_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

connect(m_ipsocket, (struct sockaddr*)&m_addr, sizeof(m_addr));
Sleep(250);

// setup the request data
string str = "<SET ID=\"ENABLE_SEND_TIME\" STATE=\"1\" />\r\n";     // send time
send(m_ipsocket, str.c_str(), str.length(), 0);
}

(this is another function, after variable has been declared)

void startreceiving()
{
string str = "<SET ID=\"ENABLE_SEND_DATA\" STATE=\"1\" />\r\n"; // start to sending data
send(m_ipsocket, str.c_str(), str.length(), 0);
}

in the startreceiving function, the m_ipsocket remains undeclared.

1

There are 1 answers

1
harper On BEST ANSWER

The class member m_ipsocket is visible to class functions like GazeTracking::InitializeConnection. The functionstartreceiving` isn't a class member. Therfore it can't access the member of the class without specifing how you access it.

When m_ipsocket is an ordinary member. You need an instance of the class.

gazeTrack.m_ipsocket

This requires that you have an object gazeTrack. Each instance of GazeTracking has it's own instance member.

When m_ipsocket is a static member then it is the same for all instances of GazeTracking. You access it with the class name:

GazeTracking::m_ipsocket.

Don't forget to control the visibility (public/private).