Solved

error message

Posted on 2003-11-27
10
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:wilslm
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
Thx so much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now