Connecting Tech Pros Worldwide Help | Site Map

Wrapping C functions in C++

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 28th, 2006, 11:05 AM
Anonymous
Guest
 
Posts: n/a
Default Wrapping C functions in C++

Hi Folks,

I want to wrap the Unix socket library in a C++ class and am having the
following difficulty. I have a method in my C++ class named connect and
within this method I want to call the Unix socket connect function. The
problem is that the compiler complains:

.../common/Socket.h:71: error: no matching function for call to
âSocket::connect(int&, sockaddr*&, int&)â
.../common/Socket.h:37: note: candidates are: void Socket::connect(const
char*, int)

Notice that it is looking within my Socket class for connect(int, sockaddr,
int), but it really should be looking in the C-based socket library. What
I've done as a kludgy fix is to create a forwarding function:

fConnect(int, sockaddr, int) {
connect(int, sockaddr, int);
}

class Socket {
connect() {
fConnect(int, sockaddr, int);
}
}

But I find this to be ugly. Is there a better way to accomplish this,
perhaps my simply specifying which connect I want to call within the class,
instead of having to deal with renaming things?

Thank you.



  #2  
Old September 28th, 2006, 11:35 AM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Wrapping C functions in C++

Anonymous wrote:
Quote:
class Socket {
connect() {
fConnect(int, sockaddr, int);
}
}
>
This has nothing to do with connect being a C function.
It has entirely everything to do with names in different
scopes. Inside Socket::connect() the lookup yields
Socket::connect(). If you want the global namespace
connect, all you do is add the scope resolution operator:


connect() {
::connect(int, sockaddr, int);
}
  #3  
Old September 28th, 2006, 11:35 AM
Zara
Guest
 
Posts: n/a
Default Re: Wrapping C functions in C++

On Thu, 28 Sep 2006 06:14:31 -0500, "Anonymous" <Anonymous@home.net>
wrote:
Quote:
>Hi Folks,
>
>I want to wrap the Unix socket library in a C++ class and am having the
>following difficulty. I have a method in my C++ class named connect and
>within this method I want to call the Unix socket connect function.
<...>
Quote:
>
>class Socket {
connect() {
fConnect(int, sockaddr, int);
use instead:
::connect(int,sockaddr,int);
Quote:
}
>}
This tells the compiler tyo look for connect at the global scope.

Once you have corrected all errros that will arise from trying to use
the token "int" as parameter to call a function and some similiar
ones... you will have your connect function working.

Zara
  #4  
Old September 28th, 2006, 02:55 PM
mlimber
Guest
 
Posts: n/a
Default Re: Wrapping C functions in C++

Anonymous wrote:
Quote:
Hi Folks,
>
I want to wrap the Unix socket library in a C++ class and am having the
following difficulty. I have a method in my C++ class named connect and
within this method I want to call the Unix socket connect function. The
problem is that the compiler complains:
>
../common/Socket.h:71: error: no matching function for call to
âSocket::connect(int&, sockaddr*&, int&)â
../common/Socket.h:37: note: candidates are: void Socket::connect(const
char*, int)
>
Notice that it is looking within my Socket class for connect(int, sockaddr,
int), but it really should be looking in the C-based socket library. What
I've done as a kludgy fix is to create a forwarding function:
>
fConnect(int, sockaddr, int) {
connect(int, sockaddr, int);
}
>
class Socket {
connect() {
fConnect(int, sockaddr, int);
}
}
>
But I find this to be ugly. Is there a better way to accomplish this,
perhaps my simply specifying which connect I want to call within the class,
instead of having to deal with renaming things?
Yes, explcitly qualify the socket's connect function as being in the
global namespace:

::connect( someInt, someAddr, someOtherInt );

Cheers! --M

  #5  
Old September 29th, 2006, 02:55 AM
Martin Steen
Guest
 
Posts: n/a
Default Re: Wrapping C functions in C++

Anonymous wrote:
Quote:
Hi Folks,
>
I want to wrap the Unix socket library in a C++ class and am having the
following difficulty. I have a method in my C++ class named connect and
within this method I want to call the Unix socket connect function.
try ::connect() to call the global function.

-Martin
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.