?
Solved

H:\C++\Hello\hello.cpp(55) : error C2440: 'return' : cannot convert from 'char [255]' to 'char'

Posted on 2003-03-05
8
Medium Priority
?
854 Views
Last Modified: 2007-12-19
I'm getting

H:\C++\Hello\hello.cpp(55) : error C2440: 'return' : cannot convert from 'char [255]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

how do I change the type to return
0
Comment
Question by:ROURKAS
8 Comments
 
LVL 1

Expert Comment

by:Linzer
ID: 8076010
Your function (maybe it's main()) must be defined as :

char MyFunction(etc.)

and you must have written : return MyString;
where MyString is an array of char.

Instead, declare (and define) your function as :
char *MyFunction(etc.)

and then use :

return static_cast<char *>(MyString);

to cast your array into a char pointer.
0
 

Author Comment

by:ROURKAS
ID: 8076126
what about for

char APIENTRY function1( char strSQL) ??
0
 

Author Comment

by:ROURKAS
ID: 8076359
heres the complete code

#include <iostream>
#include <string>
#include "stdafx.h"
#include "hello.h"
#include "stdio.h"


BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                          )
{
    switch (ul_reason_for_call)
     {
          case DLL_PROCESS_ATTACH:
          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
               break;
    }
    return TRUE;
}


char APIENTRY function1( char strSQL)
{

     char returnstring[255];
     char string[255];

        sprintf(string,"%s",strSQL);

ReplaceTerm(string, returnstring, ",'", ",~");
ReplaceTerm(string, returnstring, "',", "~,");
ReplaceTerm(string, returnstring, "')", "~)");
ReplaceTerm(string, returnstring, "('", "(~");
ReplaceTerm(string, returnstring, "'", "''");

ReplaceTerm(string, returnstring, ",~", ",'");
ReplaceTerm(string, returnstring, "~,", "',");
ReplaceTerm(string, returnstring, "~)", "')");
ReplaceTerm(string, returnstring, "(~", "('");

//char thereturn = *returnstring;
//return static_cast<char *>(returnstring);

           return returnstring;
//}

 }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 49

Expert Comment

by:DanRollins
ID: 8076632
change to:

string APIENTRY function1( char* strSQL)
{
        ...
        return returnstring;
}

-- Dan
0
 

Author Comment

by:ROURKAS
ID: 8077011
now i'm getting

h:\c++\hello\hello.h(2) : error C2143: syntax error : missing ';' before '__stdcall'
h:\c++\hello\hello.h(2) : error C2501: 'string' : missing storage-class or type specifiers
H:\C++\Hello\hello.cpp(29) : error C2143: syntax error : missing ';' before '__stdcall'
H:\C++\Hello\hello.cpp(29) : error C2501: 'string' : missing storage-class or type specifiers
H:\C++\Hello\hello.cpp(29) : error C2086: 'string' : redefinition
H:\C++\Hello\hello.cpp(51) : error C2440: 'return' : cannot convert from 'char [255]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
H:\C++\Hello\hello.cpp(54) : error C2617: 'function1' : inconsistent return statement
        h:\c++\hello\hello.h(2) : see declaration of 'function1'



here is the contents of the header file

string APIENTRY function1(char*);

 
0
 
LVL 1

Expert Comment

by:frogger1999
ID: 8077054
change the function like this

char * APIENTRY function1(char* strSQL)
{

    char returnstring[255];
    char string[255];
    sprintf(string,"%s",strSQL);

ReplaceTerm(string, returnstring, ",'", ",~");
ReplaceTerm(string, returnstring, "',", "~,");
ReplaceTerm(string, returnstring, "')", "~)");
ReplaceTerm(string, returnstring, "('", "(~");
ReplaceTerm(string, returnstring, "'", "''");

ReplaceTerm(string, returnstring, ",~", ",'");
ReplaceTerm(string, returnstring, "~,", "',");
ReplaceTerm(string, returnstring, "~)", "')");
ReplaceTerm(string, returnstring, "(~", "('");

//char thereturn = *returnstring;
//return static_cast<char *>(returnstring);

          return returnstring;
//}
0
 
LVL 1

Accepted Solution

by:
frogger1999 earned 480 total points
ID: 8077087
change the function like this

//changed the return type to char *
char * APIENTRY function1(char* strSQL)
{
    //you have to allocate it here on the heap so
    //it won't go out of scope on return
    char * returnstring = new char[255];
   
    char string[255];
    sprintf(string,"%s",strSQL);

    ReplaceTerm(string, returnstring, ",'", ",~");
    ReplaceTerm(string, returnstring, "',", "~,");
    ReplaceTerm(string, returnstring, "')", "~)");
    ReplaceTerm(string, returnstring, "('", "(~");
    ReplaceTerm(string, returnstring, "'", "''");

    ReplaceTerm(string, returnstring, ",~", ",'");
    ReplaceTerm(string, returnstring, "~,", "',");
    ReplaceTerm(string, returnstring, "~)", "')");
    ReplaceTerm(string, returnstring, "(~", "('");
   
    return returnstring;
    }

//you must remember to delete the buffer allocated when you are through with it outside this function

a different approach is to write the function as follows

void APIENTRY function1(char* strSQL, char * returnstring);

with the same body except the allocation (new) and the return statement.  Also you have to pass in an already  allocated buffer like this


void caller()
{
  char returnString[255];
  char strSQL[255];
  function1((char *) strSQL, (char *) returnString);
}
0
 

Author Comment

by:ROURKAS
ID: 8085556
Cheers mate!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

616 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