LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ ERROR: int to const char (https://www.linuxquestions.org/questions/programming-9/c-error-int-to-const-char-214310/)

ashirazi 08-06-2004 12:40 PM

c++ ERROR: int to const char
 
Hey guys,

Thanks a lot for all the replies for the previous posts. I get a "Cannot convert 'int' to 'const char' " error, when i try to implement this inline function. My main() just has the following declarations: int ns, int* which_hill, and argv[] is an array of the parameters to call main().
The following is the function.

Thanks a million guys
Aga




inline void read_args(int argc, char argv[], int &ns, int *which_hill) {

/* Check if only specified partitions are to be processed. */
for (int i=0; i<argc; i++) {

if (strcmp(argv[i],"-x") == 0) {
xflag = 1;
/* Check the next arguement to see the number of partitions we will process. */

if (i+1 >= argc) {
cout << "\nERROR: Number of partitions not specified with -x option";
exit(0);
}

i++;
ns = (int)atoi(argv[i]);

if (i+ns >= argc) {
cout << "\nERROR: Number of given partitions does not match specification with -x option";
exit(0);
}

/* Allocate an array indexing partition numbers processed */
if ( (which_hill = (int *)malloc(ns*sizeof(int))) == NULL) {
cout << "\nERROR: Memory allocation problem\n";
exit(0);
}

/* Read in successive arguements until we fill up the partition array. */
for (int k=0; k< ns; ++k) {
i++;
which_hill[k] = (int)atoi(argv[i]);
}
}
}

ashirazi 08-06-2004 12:51 PM

I forgot to mention, I get the error whereever argv is used

Charalambos 08-06-2004 02:07 PM

What type has the variable xflag? And where is it declared?

ashirazi 08-06-2004 04:58 PM

sorry about that, thats passed as int. I just realized that

Charalambos 08-06-2004 05:02 PM

Quote:

"Cannot convert 'int' to 'const char' " error
In whick line does that error occur?

aiza 08-06-2004 09:58 PM

Re: c++ ERROR: int to const char
 
Quote:

Originally posted by ashirazi
Hey guys,

Thanks a lot for all the replies for the previous posts. I get a "Cannot convert 'int' to 'const char' " error, when i try to implement this inline function. My main() just has the following declarations: int ns, int* which_hill, and argv[] is an array of the parameters to call main().
The following is the function.

Thanks a million guys
Aga




inline void read_args(int argc, char argv[], int &ns, int *which_hill) {

/* Check if only specified partitions are to be processed. */
for (int i=0; i<argc; i++) {

if (strcmp(argv[i],"-x") == 0) {
xflag = 1;
/* Check the next arguement to see the number of partitions we will process. */

if (i+1 >= argc) {
cout << "\nERROR: Number of partitions not specified with -x option";
exit(0);
}

i++;
ns = (int)atoi(argv[i]);

if (i+ns >= argc) {
cout << "\nERROR: Number of given partitions does not match specification with -x option";
exit(0);
}

/* Allocate an array indexing partition numbers processed */
if ( (which_hill = (int *)malloc(ns*sizeof(int))) == NULL) {
cout << "\nERROR: Memory allocation problem\n";
exit(0);
}

/* Read in successive arguements until we fill up the partition array. */
for (int k=0; k< ns; ++k) {
i++;
which_hill[k] = (int)atoi(argv[i]);
}
}
}

I think you missed '*' sign before 'argv[]' in function definition. It should be:
inline void read_args(int argc, char *argv[], int &ns, int *which_hill)
{
......
}

ashirazi 08-06-2004 10:01 PM

Thanks man I fixed it. I wasnt passing the arry right.

Thanks a lot anyway


All times are GMT -5. The time now is 02:20 AM.