Link to home
Start Free TrialLog in
Avatar of wilslm
wilslm

asked on

error message

I'm getting this error message:

error C2664  'MyFunction' : cannot convert parameter 1 from 'std::allocator<_Ty>::value_type' to 'char *'     with     [         _Ty=char        ]


what is this error trying to tell me?
i'm using visual c++

thx.
tejo
Avatar of Sys_Prog
Sys_Prog
Flag of India image

Looking at the error, it seems that

Your MyFunction () function expects the first parameter to be a char *, but u are psssing something else ( which the compiler cannot convert to char * ) while invoking MyFunction().

You can Post your code so that we can help out in a better way

HTH

Amit
Avatar of wilslm
wilslm

ASKER

MyFunction's first parameter is char *.
prototype:
void MyFunction (char *);



the commands i have :
string * test = new test [5];

for (int i = 0 ; i < 5 ; i ++ )
{
      if(i=0)
        test[i]=" ";
      else
        test[i]+=" ";
}

MyFunction ( test[2][3] );


i got that error when i run this
A couple of observations

>>string * test = new test [5];

This should be
string * test = new string [5];         // Allocate a pointer of type of string to hold an array of size 5 for string type objects

Then, your function expects a char * type of parameter.

and the compiler cannot convert when u say

MyFunction ( test[2][3] );

Instead u will have to say

MyFunction ( test[2].c_str() ) ;

This also would give u an error saying that cannot convert const char to *char*.

That's c_str() gives u const char *. Thus your function prototype should be

MyFunction ( const char * );

Go thru the following code for exampe

#include <iostream>

using namespace std;

void f ( const char *p ) {
  cout << "f()" << endl ;
  cout << p ;
}
int main () {
      string *test = new string [5] ;
      test[0] = "testing" ;
       f ( test[0].c_str() ) ;      
       system ( "PAUSE" ) ;
}

HTH

Amit

Avatar of wilslm

ASKER

no no.. i got you point but thats not exactly what i want. i'm not passing the whole string.

say for example:

i defined
string *test = new string[5];

then i managed to put the values into the string, such that
test[0] = "ABCDE";
test[1] = "FGHIJ";
test[2] = "KLMNO";
test[3] = "PQRST";
test[4] = "UVWXY";


by passing test[2][3], i mean i'm passing 'N' into the function.

thx alot btw :)
Then why do u need char * in your function prototype

You just need char

Thus your function should be

MyFunction ( char p ) ;

Example

#include <iostream>

using namespace std;

void f ( char p ) {
  cout << "f()" << endl ;
  cout << p ;
}
int main () {
      string *test = new string [5] ;
      test[0] = "amit" ;
       f ( test[0][1] ) ;      
       system ( "PAUSE" ) ;
}


HTH

Amit
Is the compiler settings set up to use w_char instead of char? If that is the case, you are trying to pass in a w_char  to your function (unbeknownst to you). And also, you're not actually passing a char *, you're passing a char (or w_char as it may be)!

Try changing your function to accept a char instead of a char*.

Good luck!
Avatar of wilslm

ASKER


function prototype cant change. it has to accept char*. its a must.

how do i pass the character from test[2][3] to taht function?
ASKER CERTIFIED SOLUTION
Avatar of Sys_Prog
Sys_Prog
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The first sol is useful if u cannot modify the function

Example for first

#include <iostream>

using namespace std;

void f ( char * p ) {
  //*(p+1) = '\0' ;                        
  cout << "f()" << endl ;
  cout << p ;
}
int main () {
      string *test = new string [5] ;
      char temp [2] = { '\0' } ;
      test[0] = "amit" ;
      temp[0] = test[0][1] ;
       f ( temp ) ;      
       system ( "PAUSE" ) ;
}


HTH
Amit
Avatar of wilslm

ASKER

Thx so much