?
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
?
831 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
[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
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

Technology Partners: 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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

762 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