By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

Socket program in c++. gives "‘read’ was not declared " error on read () system call

P: 28
Hi guys I wrote a socket program in c++ and used blocking system call read () for receiving data on socket. But it gives following error while compilation

[HTML]g++ -Wall -g -I`pwd`/../include -lpthread -c SocketDescriptor.cc -o `pwd`/../lib/SocketDescriptor.o
SocketDescriptor.cc: In member function ‘Status SocketDescriptor::Recv(int&, unsigned char*, int&)’:
SocketDescriptor.cc:79: error: ‘read’ was not declared in this scope
make: *** [SocketDescriptor.o] Error 1[/HTML]

definition of this read () call is:
int count read (int socketFd, char * buffer, int bufferlength, int flags)

When use the same call in a C programe I don't get this error. Moreover programe works fine. I think C library uses certain default headers and C++ do not.

Do anybody has any idea? Or please tell how I can look for it.


Thanks
Nov 19 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 671
Let's see some code.

Here's the first thought that passed into my head. You didn't include the header properly. C implicitly assumes a function returns an int. I.e., you could potentially get away with a lot of bad code, because Wall won't trap everything. C++ is more strict though.
Nov 19 '07 #2

P: 28
Following are the headers I have included in both the programs.
#include <netinet/in.h>
#include <linux/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

C code:
13 memset(buff, '\0', BUFFLEN);
14 if (read(sock, buff, BUFFLEN, flags) < 0){/*error handling*/}
15 printf("[SERVER] %s", buff);

C++ code:

75 Status SocketDescriptor::Recv(Int32 & msgType, UInt8* data, Int32 & data len)
76 {
77 UInt8 *buff = new UInt8[MAX_DATA_LEN];
78 Int32 len, totallen;
79 if ( read(descriptor, buff, len, 0) < 0)
80 {
81 return ERROR;
82 }

It would be a great help if you are able resolve it. From two days I am looking for it.

Thanks
Nov 22 '07 #3

P: 28
Hi I figured it out in a way.
I added an h and c file to my code having wrappers over read and write sys calls. Compiled them with C compiler. And used the c header in the code where I needed read and write with C lang linkage specifier.
It works fine now.
Nov 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.