mitchguy
asked on
linux c++ calling global networking function connect( ) from inside class, but compiler cant find it
I'm trying to add a tcp client to a pre-existing class.
I compiled a book tutorial client example and then when I put it in my
class it won't compile.
no matching function for call to 'myClass::connect(int&, sockaddr*,int&);'
candidates are: void myClass::connect()
which makes sense because that function is not in my class with those parameters.
The problem seems to be that I'm using an api which my class is derived from, which required me to define a function called connect() in my class
the program is to big to put the code here but i've replicated the problem i need to solve with a small test program.
if I comment out the local connect declaration and definition it compiles fine.
why is it having a scope problem? one has parameters and one doesn't, so they are different
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream.h>
class::javaClient
{
public:
javaClient();
void createClient();
void connect();
};
void javaClient::createClient()
{
int sockfd, len, result;
struct sockaddr_in address;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address_sin_port = htons(5010);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("error connecting client");
}
}
void javaClient::connect()
{
cout<<"class connect()"<<endl;
}
int main()
{
javaClient jc;
jc.createClient();
}
I compiled a book tutorial client example and then when I put it in my
class it won't compile.
no matching function for call to 'myClass::connect(int&, sockaddr*,int&);'
candidates are: void myClass::connect()
which makes sense because that function is not in my class with those parameters.
The problem seems to be that I'm using an api which my class is derived from, which required me to define a function called connect() in my class
the program is to big to put the code here but i've replicated the problem i need to solve with a small test program.
if I comment out the local connect declaration and definition it compiles fine.
why is it having a scope problem? one has parameters and one doesn't, so they are different
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream.h>
class::javaClient
{
public:
javaClient();
void createClient();
void connect();
};
void javaClient::createClient()
{
int sockfd, len, result;
struct sockaddr_in address;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address_sin_port = htons(5010);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("error connecting client");
}
}
void javaClient::connect()
{
cout<<"class connect()"<<endl;
}
int main()
{
javaClient jc;
jc.createClient();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
wow, twenty minutes to type in the question, 30 seconds to solve it.
Thanks
Thanks
ASKER
result = connect(sockfd, (struct sockaddr *)&address, len);
it finds it no problem with these commented out
//void connect();
//void javaClient::connect()
//{
//cout<<"class connect()"<<endl;
//}