ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am looking for some help compiling c++ code using g++ on slackware 10.1 ( compiler version 3.4.3 I think ).
I have included code for last error and warning I have not been able to answer.
1) Can someone tell me what "shadowing a parameter" means ?
2) If i understand correctly, sleep( val ) can take floating point as input, because of this I do not understand the warning, especially because the input parameter for sleep is being passed in to the member function as a float .
And in the header file the member function call is defined with float type input.
3) I also do not understand what the error "multiple types in one declaration" means in the header file.
Thanks in advance for your help.
I am compiling 2 files and i get these errors :
bash-3.00# g++ main.cpp port.cpp -o resul
In file included from main.cpp:1:
port.h:142: error: multiple types in one declaration
In file included from port.cpp:10:
port.h:142: error: multiple types in one declaration
port.cpp: In member function `int OSPort::sendchar(char)':
port.cpp:267: error: declaration of `charToSend' shadows a parameter
port.cpp: In member function `int OSPort::delay_time(float)':
port.cpp:315: warning: converting to `unsigned int' from `float'
bash-3.00#
int OSPort :: sendchar(char charToSend )
{
int num;
char charToSend[255];
num = write(fd, charToSend, 100); ///////// Line 267
if (num < 0)
fputs("sendchar() failed\n", stderr);
}
int OSPort :: delay_time (float dtime )
{
int res;
int sleepval;
sleepval = sleep (dtime); ///////// Line 315
if ( sleepval != 0) { // failure, sleep() returns !0
perror("function :: delay_time error -- \n");
res = 1;
}
else // success, sleep() returns 0
res = 0;
I've never written a single char using write, I'm pretty new to using it, but I think you should pass the address of the character as I showed above in red.
As for your sleep(...) error, I'm pretty sure the parameter is an unsigned int (seconds). Actually i'm looking at my man page now (Slackware) and it does in fact recieve an unsigned parameter, and returns an unsigned value. The return value is 0 if requested time has elapsed, otherwise its the number of seconds left to sleep.
But that is a compiler warning, not an error. If you post the lines around 142 you can fix the errors and get your program running. I would recommend changing the sleep parameter though, getting used to compiler warnings can get sloppy after a while.
If do "man 1 sleep" it says float values can be used :
from "man 1 sleep"
Description :
Pause for NUMBER seconds. SUFFIX may be `s' for seconds
(the default), `m' for minutes, `h' for hours or `d' for
days. Unlike most implementations that require NUMBER be
an integer, here NUMBER may be an arbitrary floating point
number.
Maybe I should use usleep or Tcl_Sleep ?
These functions delay for milliseconds. I am using this for a delay timer to control a serial port.
bash-3.00# g++ main.cpp port.cpp -o resul
In file included from main.cpp:1:
port.h:142: error: multiple types in one declaration
... <= WE NEED TO SEE LINE 142 FROM "PORT.H" TO DETERMINE THE ERROR
My guess is the erroneous code might look something like this:
Code:
int
main (int argc, char *argv[])
{
int char foo;
...
cc -o foo foo.c =>
foo.c: In function `main':
foo.c:6: error: two or more data types in declaration of `foo'
2. Shadow error:
Code:
port.cpp: In member function `int OSPort::sendchar(char)':
port.cpp:267: error: declaration of `charToSend' shadows a parameter
...
int OSPort :: sendchar(char charToSend )
{
int num;
char charToSend[255];
num = write(fd, &charToSend, 100);
The compiler is asking you if you want to use the "charToSend" you passed in as a paramter, or the one you declared locally. My guess is that you probably don't need the local variable (i.e. you can just lose the "char charToSend[255];" line).
3. Converting unsigned int to float:
Code:
port.cpp: In member function `int OSPort::delay_time(float)':
port.cpp:315: warning: converting to `unsigned int' from `float'
...
int OSPort :: delay_time (float dtime )
{
int res;
int sleepval;
sleepval = sleep (dtime); ///////// Line 315
<= THE COMPILER'S LETTING YOU KNOW THAT "sleep()" TAKES AN INTEGER, AND THAT
IT'S GOING TO CONVERT YOUR FLOAT TO AN INTEGER ON YOU
One way around the warning is simply to do a cast:
Code:
POSSIBLE ALTERNATIVE:
nt OSPort :: delay_time (float dtime )
{
int res;
int sleepval;
sleepval = sleep ((unsigned long)dtime); ///////// Line 315
'Hope that helps .. PSM
ADDENDUM:
You were typing a response while I was making this post.
The "man" page does NOT say that you can pass a "float" into the C library function "sleep()".
"man 1 sleep" is for shell command "sleep".
"man 3 sleep" is the one you're interested in: for the library API "sleep()".
PS:
When I compiled your class definition (adding "#define PORTNAMEALLOC 255" first), it compiled without error. I did *not* get any "multiple types" errors or warnings from your code.
If do "man 1 sleep" it says float values can be used :
from "man 1 sleep"
Description :
Pause for NUMBER seconds. SUFFIX may be `s' for seconds
(the default), `m' for minutes, `h' for hours or `d' for
days. Unlike most implementations that require NUMBER be
an integer, here NUMBER may be an arbitrary floating point
number.
You are confusing the section 1 sleep command with the section 3 sleep function. The section 1 commands are the commands that you may execute from the command line. Section 3 functions are the C/C++ library functions that you may call from within your program.
To instruct the man utility which section to use simply execute man section_number label. For example; executing "man sleep" will (by default) display the section 1 man page for the sleep command. Executing "man 3 sleep" will display the section 3 man page for the sleep function.
There are a number of sections, and each of them have their own documents. Some of the document names are duplicated, so you need to be careful in how you call man.
//*************************************************************************
class OSPort
//************************************************************************
// hEvent = createEvent (
// NULL, // default security attributes
// FALSE, // AUTO RESET EVENT
// TRUE, // signaled
// NULL // no name
// );
// if (!hEvent)
// return;
// } // end AssertEsc()
/////////////////////////////////////////////////////////////////////////////
// Name : openPort
// Description : Opens serial port and sets for raw output.
/////////////////////////////////////////////////////////////////////////////
int OSPort :: openPort(void)
{
int ret;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.