Solved

error message

Posted on 2003-11-27
10
291 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 67
C++ dll will not run on machine without VS installed 10 86
C++ :Change value from  DisableCMD registry 4 60
Issues with C++ Class 19 92
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 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