Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-07-2005, 06:10 PM
|
#1
|
LQ Newbie
Registered: Apr 2004
Distribution: Slackware & Gentoo & Ubuntu
Posts: 22
Rep:
|
c++ compile errors using g++
Howdy,
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;
return res;
}
|
|
|
09-07-2005, 07:32 PM
|
#2
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340
Rep:
|
Re: c++ compile errors using g++
First can you post line 142 (multiple types in one definition thing...)? That will help solve the first errors with the multiple type mismatch error.
Code:
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);
}
Look at the prototype for write(...)
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
man 2 write
http://www.die.net/doc/linux/man/man2/write.2.html
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.
|
|
|
09-07-2005, 08:12 PM
|
#3
|
LQ Newbie
Registered: Apr 2004
Distribution: Slackware & Gentoo & Ubuntu
Posts: 22
Original Poster
Rep:
|
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.
*****************************************************************
Below is line 142 of port.h :
class OSPort
{
// defines for options setting
#define EVEN_PARITY 2
#define ODD_PARITY 1
#define NO_PARITY 0
#define SIMULATED_PARITY -1
#define STOP_BITS2 2
#define STOP_BITS1 0
typedef struct _LINECONTROLSET {
unsigned char bDataBits;
unsigned char bParity;
unsigned char bStopBits;
} LINECONTROLSET;
private :
int bcount;
char InputBuffer [60];
int outpos;
protected:
enum pstatus {PORTCLOSED, PORTOPEN, NEEDSRESET};
int portstatus;
char portname [PORTNAMEALLOC];
int fd; // HANDLE porthandle
// ULONG ulParmLen;
// ULONG ulAction;
// ULONG ulDataLength;
unsigned long action;
int rc;
int NoBaud;
unsigned int CurrentBaudrate;
int Bits;
bool Emulate7E1;
int Parity;
char filler1 [5];
// DCB PortDCB;
char filler2 [60];
unsigned short statusReturned;
public :
OSPort ();
int carrierDrop (void);
int carrierDetect (void);
int closePort (void);
int delay_time ( float );
// void displaySystemError (DWORD err);
void dropDTR (void);
int GetBaudRate () { return CurrentBaudrate; };
bool getEmulate7E1(){ return Emulate7E1;}
void FlushPort (void);
int openPort ();
int rcvchar (void);
void raiseDTR (void);
int sendchar (char charToSend);
int SetBaudrate (int baud);
void setNeedsReset (void) { portstatus = NEEDSRESET; };
void SetPortName (char * portn);
char SetParity(int ParityType, char c);
void SetStringParity(int ParityType, char *packet, int n);
int SetNoCTS (void);
int SetBits (int Bit);
int startTimer ( int time);
// int timedOut (int Base, int timeOut );
int waitForXmit ();
int WriteBuf(char* buf, unsigned long datalen, unsigned long &byteswritten);
void SetstringParity(int, char *, int );
// void AssertEsc();
}; ////////////////// Line 142
Thank You for the help.
|
|
|
09-07-2005, 08:23 PM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Hi -
1. Multiple types error:
Code:
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.
Last edited by paulsm4; 09-07-2005 at 08:32 PM.
|
|
|
09-08-2005, 11:47 AM
|
#5
|
Member
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205
Rep:
|
Hi,
Quote:
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.
|
|
|
09-08-2005, 02:58 PM
|
#6
|
LQ Newbie
Registered: Apr 2004
Distribution: Slackware & Gentoo & Ubuntu
Posts: 22
Original Poster
Rep:
|
I still do not see what is causing this error :
In file included from port.cpp:10:
port.h:142: error: multiple types in one declaration
Here is the entire file port.h :
#define LASTBYTE(s) (s + (sizeof(s) - 1))
#define TRIES_3 3
#define max_retries 3
#define DEFAULT_PORTNUM 1
#define DEFAULT_BAUDRATE 1200
#define BAUDSTRING "1200"
#define PORTNAMEALLOC 16
// ASCII Protocol Characters
#define NUL 0
#define SOH 1
#define STX 2
#define ETX 3
#define EOT 4
#define ENQ 5
#define ACK 6
#define BEL 7
#define BS 8
#define HT 9
#define LF 0x0A
#define VT 0x0B
#define FF 0x0C
#define CR 0x0D
#define SO 0x0E
#define SI 0x0F
#define DLE 0x10
#define DC1 0x11
#define DC2 0x12
#define DC3 0x13
#define DC4 0x14
#define NAK 0x15
#define SYN 0x16
#define ETB 0x17
#define CAN 0x18
#define EM 0x19
#define SUB 0x1A
#define ESC 0x18
#define FS 0x1C
#define GS 0x1D
#define RS 0x1E
#define US 0x1F
// ASCII Protocol Characters
#define XON 0x11
#define XOFF 0x13
// VISA 2 protocol chars
#define vACK 0x07 // Visa ACK 1
#define vWACK 0x11 // Visa Wait ACK
#define vWAIT 0x12 // Visa Wait
// Return Codes for the VISA subroutines
#define VISA_OK 0
#define VISA_ERR -1
#define VISA_ESC -2
#define VISA_STX -3
#define VISA_BUSY -4
#define VISA_EMPTY -5
#define VISA_DROP -6
#define VISA_EOT -7
#define VISA_RESET -8
#define VISA_TIMEOUT -9
int visasizeof (char * ptr);
//*************************************************************************
class OSPort
//************************************************************************
class OSPort
{
// defines for options setting
#define EVEN_PARITY 2
#define ODD_PARITY 1
#define NO_PARITY 0
#define SIMULATED_PARITY -1
#define STOP_BITS2 2
#define STOP_BITS1 0
typedef struct _LINECONTROLSET {
unsigned char bDataBits;
unsigned char bParity;
unsigned char bStopBits;
} LINECONTROLSET;
private :
int bcount;
char InputBuffer [60];
int outpos;
protected:
enum pstatus {PORTCLOSED, PORTOPEN, NEEDSRESET};
int portstatus;
char portname [PORTNAMEALLOC];
int fd; // HANDLE porthandle
// ULONG ulParmLen;
// ULONG ulAction;
// ULONG ulDataLength;
unsigned long action;
int rc;
int NoBaud;
unsigned int CurrentBaudrate;
int Bits;
bool Emulate7E1;
int Parity;
char filler1 [5];
// DCB PortDCB;
char filler2 [60];
unsigned short statusReturned;
public :
OSPort ();
int carrierDrop (void);
int carrierDetect (void);
int closePort (void);
int delay_time ( int );
// void displaySystemError (DWORD err);
void dropDTR (void);
int GetBaudRate () { return CurrentBaudrate; };
bool getEmulate7E1(){ return Emulate7E1;}
void FlushPort (void);
int openPort ();
int rcvchar (void);
void raiseDTR (void);
int sendchar (char charToSend);
int SetBaudrate (int baud);
void setNeedsReset (void) { portstatus = NEEDSRESET; };
void SetPortName (char * portn);
char SetParity(int ParityType, char c);
void SetStringParity(int ParityType, char *packet, int n);
int SetNoCTS (void);
int SetBits (int Bit);
int startTimer ( int time);
// int timedOut (int Base, int timeOut );
int waitForXmit ();
int WriteBuf(char* buf, unsigned long datalen, unsigned long &byteswritten);
void SetstringParity(int, char *, int );
// void AssertEsc();
}; ////////////// <------- Line 142
//***************************************************************************
extern int fd;
extern struct termios options;
extern int status;
*****************************************************
*****************************************************
top few lines of port.cpp :
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/ioctl.h>
#include <linux/serial_reg.h>
#include <string.h>
#include "port.h" /////// <-- Line 10 of port.cpp
using namespace std;
#define PORTDEVICE "/dev/ttyS0"
/////////////////////////////////////////////////////////////////
// class OSPort
OSPort :: OSPort ()
{
portstatus = PORTCLOSED;
NoBaud = 0;
Emulate7E1 = false;
Parity = NO_PARITY;
Bits = 8;
} // end OSPort() // removed ;
// void OSPort :: AssertEsc()
//{
// HANDLE hEvent;
// create an event object
// 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;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
if (fd == -1) {
perror("openPort: unable to open /dev/ttyS0 - \n");
return ( VISA_ERR );
}
else
fcntl(fd, F_SETFL, 0 );
options.c_oflag &= ~OPOST; // raw output
ret = tcsetattr(fd, TCSANOW, &options); // set terminal attributes immediately
return ( VISA_OK );
}
|
|
|
All times are GMT -5. The time now is 11:37 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|