Solved

error message

Posted on 2003-11-27
10
292 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:wilslm
  • 5
  • 4
10 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9835325
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
0
 

Author Comment

by:wilslm
ID: 9835347
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
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9835369
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

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:wilslm
ID: 9835387
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 :)
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9835396
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
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9835419
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!
0
 

Author Comment

by:wilslm
ID: 9835481

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?
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 500 total points
ID: 9835515
I can think of 2 sols

1. Copy that char into a char * and append it with '\0' and then pass this resultant into the function


2. If u can change the function, then Pass the address of the char i.e. &test[0][1] and then in the function, append the char with a '\0'.

Example

#include <iostream>

using namespace std;

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

HTH
Amit  
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9835526
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
0
 

Author Comment

by:wilslm
ID: 9844470
Thx so much
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question