Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

linux c++ calling global networking function connect( ) from inside class, but compiler cant find it

Posted on 2004-08-25
3
Medium Priority
?
308 Views
Last Modified: 2010-04-01
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();
}

0
Comment
Question by:mitchguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:mitchguy
ID: 11896820
this is the connect it cant find, while I have my own class connect() defined
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;
//}

0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 11896859
Try to use

 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); // <-- added '::'
 if(result == -1)
  {
   perror("error connecting client");
  }
}

to indicate that you mean the 'global 'connect()'
0
 

Author Comment

by:mitchguy
ID: 11896897
wow, twenty minutes to type in the question, 30 seconds to solve it.
Thanks
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question