ctocpp
ctocpp

Reputation: 23

Could not understand error message when converting code from C to C++

I am converting a c program into c++, and I encountered these error message:

error on line 57:
error: invalid conversion from ‘int*’ to ‘socklen_t*’
error: initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’

My code at line 57 is:

connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);

i am not sure if this information is enough for your guys to figure out. The total no of codes is few hundreds, should I paste it here?

Additional information:

I am doing socket programming in linux environment

Thanks to Andreas Brinck

There is no code line 57 error. But now, I got this:

warning: the `gets' function is dangerous and should not be used.
/tmp/ccBDefaZ.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

EDIT Second time:

I could ignore the warning, but the:

/tmp/ccBDefaZ.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

What does it mean? Can it be fixed?

Problem solved.

Previously, I use gcc to compile, then I got the error message, when I switch to g++, the error messages are gone. Thank you guys =)

Upvotes: 2

Views: 1873

Answers (5)

Giuliano
Giuliano

Reputation: 192

About the "warning: the 'gets' function is dangerous and should not be used."

It appears that gets() is being "deprecated"... It is actually 'dangerous' because it allows yout to read more data than you have room allocated.

Use fgets instead, it should be fine. fgets() works like

fgets(array, array_length, filestream);

In other words, a string to fill, its length and the file from where the data must come from. To substitude gets(), the file is stdin, the standard input file.

Hope it helps.

Upvotes: 0

Manoj R
Manoj R

Reputation: 3247

From this error I can deduce that the socklen_t type is different than the int type. And your function accept expects the pointer of socklen_t type. Either change the signature of accept to make it similar to int or if you are sure that socklen_t will never be different than int(long int, or unsigned int etc.), then use proper cast while passing it to accept.

Upvotes: 0

lijie
lijie

Reputation: 4871

sin_size is probably defined as an int. However, accept requires a socklen_t *. That's the interpretation of the error.

You may be able to get away by casting. But try using a socklen_t instead, for sin_size.

C++ is rather more fussy with types.

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122401

Convert sin_size to type socklen_t:

socklen_t sin_size = 0;

Upvotes: 2

Andreas Brinck
Andreas Brinck

Reputation: 52549

Try:

connected = accept(sock, (struct sockaddr *)&client_addr, (socklen_t*)&sin_size);

sin_size is obviously an int and accept expects a socklen_t*.

Upvotes: 2

Related Questions