Solved

error message

Posted on 2003-11-27
10
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 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