0

In C++, I've got a string array variable called:

...
/* set the variable */
string fileRows[500];
...
/* fill the array with a file rows */
while ( getline(infile,sIn ) )
{
    fileRows[i] = sIn;
    i++;
}

and an object which has this:

string Data::fileName(){
    return (fileRows);
}

I would like to make a function which return an array, and after that i would like to call it something like this:

Data name(hwnd);
MessageBox(hwnd, name.fileName(), "About", MB_OK);

But i get this error:

cannot convert 'std::string* {aka std::basic_string}' to 'LPCSTR {aka const char}' for argument '2' to 'int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT)'

If i would like to show the 5. element of the array, how to convert it?

LihO
  • 41,190
  • 11
  • 99
  • 167
szuniverse
  • 1,076
  • 4
  • 17
  • 32

4 Answers4

5

LPCSTR is nothing else but an alias for const char*. The problem is that the Data::fileName() returns a std::string object and there's no implicit conversion to const char*.

To retrieve a string from the std::string in form of const char*, use c_str() method , :

MessageBox(hwnd, name.fileName().c_str(), "About", MB_OK);

Also note that you have created an array of std::string objects:

string fileRows[500];

but in the Data::fileName() you're trying to return it as a single std::string object:

string Data::fileName() {
    return fileRows;
}

I recommend you to use std::vector instead of C-style array though.

If i would like to show the 5. element of the array, how to convert it?

No matter whether you will use std::vector or keep using an array, it will look like this:

std::string Data::fileName() {
    return fileRows[4];
}
LihO
  • 41,190
  • 11
  • 99
  • 167
  • 2
    `LPCSTR` is `char const*`. :-] – ildjarn Mar 13 '13 at 21:46
  • I got this error: error: could not convert '(std::string*)(&((Data*)this)->Data::fileRows)' from 'std::string* {aka std::basic_string*}' to 'std::string {aka std::basic_string}' – szuniverse Mar 13 '13 at 21:53
  • Do you know how to convert this error, If I would like to show the 5. element of the array? – szuniverse Mar 13 '13 at 21:58
  • @David: There's another error that I overlooked before. Check my answer now :) – LihO Mar 13 '13 at 21:59
  • yes i know because i tried several ways to return a string array. I tried with: string* Data::fileName()... Data::fileName().. but none of them worked, beacause i dont know how to display an array element in the messagebox.. – szuniverse Mar 13 '13 at 22:03
  • @David: `std::vector fileRows` will allow you to treat `fileRows` almost in the same way as an array, but it will be flexible in size. I also answered your last question, check my answer now :) – LihO Mar 13 '13 at 22:19
2

fileRows is an array of 500 elements. If you want to return the array so that you can later access the n-th element, you should return the pointer to the beginning of the array. For example:

string* Data::fileName(){
        return fileRows;
}

Although it is probably better to use:

const string& Data::getFileName(size_t index){
        return fileRows[index];
}

Using the first method, you can access the n-th element using:

data.filename()[n];

So, if you want to access the 5th element of the array you should use:

data.filename()[4];

On the other hand, the function MessageBox needs a const char *. So you must call the c_str() method to get the pointer:

Data name(hwnd);
MessageBox(hwnd, name.fileName()[4].c_str(), "About", MB_OK);
J. Calleja
  • 4,855
  • 2
  • 33
  • 54
1

std::string::c_str will give you a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) or LPCSTR

Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
0

use std:string's function c_str() ... take a look at this answer

Community
  • 1
  • 1
A4L
  • 17,353
  • 6
  • 49
  • 70