Errors when porting from MSVC to GCC

closed account (Sy0XoG1T)
Due to the project I'm working on, I'm required to use a GCC compiler. The problem is, it's giving me some strange errors when compiling the code I've written in MSVC.

Like, for example, if I had a function that took a STL string reference and I passed a type casted char string, GCC would give me errors. This code would work fine in MSVC.

Here's some pseudo to illustrate what I'm talking about:
1
2
3
4
5
6
7
8
9
10
11
void OutputString(std::string & myString)
{
    //...
}

int main()
{
    char * myCharString = "Hello World!";
    OutputString((std::string)myCharString); //Works fine in MSVC, errors in GCC
    return 0;
}


It seems as though type casting doesn't return a value in GCC. Could anyone explain this?

Also, I'd like to hear your recommendations for a GCC IDE.
closed account (Sy0XoG1T)
Bump.
Posting the errors will help
invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
It something about the lifetime of the object.
The temporaries die in that line, but you can extend the life with a const reference. However you are using a non-const reference, requiring a lvalue.
i wonder why it compiles in MSVC.

Also:
1
2
//char * myCharString = "Hello World!"; //deprecated conversion from string constant to 'char*'
const char *myCharString = "Hello World!";


And please, be patient.
closed account (Sy0XoG1T)
It seems type casting in GCC returns a const value instead of a non-const value. Changing the parameter to a const reference fixes it...

1
2
3
4
void OutputString(const std::string & myString)
{
    //...
}


Here are the errors anyway...
1
2
error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
error: in passing argument 1 of 'void OutputString(std::string&)'


And I didn't bump because it was taking too long, I bumped because it got moved half way down the page. :)
Last edited on
The problem is that you can not provide temporary objects when the parameter is reference to non-constant:

void OutputString(std::string & myString)

requires non-constant string that will be acquired by reference, but the result from this conversion

(std::string)myCharString is a temporary object.

Regards

P.S. Basically what ne555 said.
Last edited on
Topic archived. No new replies allowed.