3

In msvc, I have functions like this and it builds but in gcc it doesnt like it.

void classname::a(std::string &text)
{
    stdStringFromClass = text;
}

void classname::b(char *text)
{
    a(std::string(text));
}

The issue here is in the &, gcc I think is worried that since I just created that std::string, that passing by reference is risky, so it does not build, but msvc doesnt even warn me.

Why is this incorrect c++ to gcc I keep hearing that msvc is stricter than gcc.

Thanks

The error

AguiWidgetBase.cpp: In member function ‘void AguiWidgetBase::setText(char*)’:
AguiWidgetBase.cpp:91:27: error: no matching function for call to ‘AguiWidgetBase::setText(std::string)’
AguiWidgetBase.cpp:80:6: note: candidates are: void AguiWidgetBase::setText(std::string&)
AguiWidgetBase.cpp:88:6: note:                 void AguiWidgetBase::setText(char*)

would this be okay?

void classname::a(std::string &text)
{
    stdStringFromClass = text;
}

void classname::b(char *text)
{
   std::string k = text;
    a(k);
}
5
  • Need more context. What's the error you get, what is "stdStringFromClass", etc.
    – vanza
    Oct 9, 2010 at 20:16
  • stdStringFromClass is a std::string
    – jmasterx
    Oct 9, 2010 at 20:19
  • MSVC is not more strict than gcc, that seems rather backwards to me. The error is that a temporary cannot be bound to a non-const reference. However, it's tough to tell you a solution when you've shown very little code. Do you need to modify the parameter? If not, make it a const-reference. What's the point of having a char* (should be const char*) overload along with a std::string overload? std::string can be implicitly constructed from a const char*.
    – GManNickG
    Oct 9, 2010 at 20:34
  • Really? Can you have a class named class?
    – Arun
    Oct 9, 2010 at 21:04
  • @GMan: Thanks, I knew that. I was amazed by the question text where class is consistently used as a class name. Since SO is archived, I thought that it is not a good example to archive. My comment was supposed to be humor :(, although I did not put the smiley :)
    – Arun
    Oct 9, 2010 at 22:36

4 Answers 4

6

I think this is an old compiler extension that Visual Studio supported since way back then, but it is kept around in modern Visual C++ for compatibility. Try disabling compiler extensions (/Za flag) and see what happens.

Alternatively, use the /W4 flag to get maximum warnings, and it should complain:

warning C4239: nonstandard extension used

On GCC I get the const reference error:

error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘std::string’

3

I have no idea why Visual studio allows you to compile this, but you can't pass a reference to an anonymous object.

Edit: Its ok for const reference, cause you can pass the temporary object as reference, just being able to modify it doesn't make sense. That's why we have rvalue references in C++0x.

Edit2: To your edit, yes that would fix the problem, or just take a const reference as parameter in the a() method, you don't seem to modify the parameter.

2
  • Oh, you should see some of the other stuff msvc lets me do :p
    – jmasterx
    Oct 9, 2010 at 20:17
  • const references work, though. The error I get with some guessing is: "str.cpp:11: error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘std::string’"
    – vanza
    Oct 9, 2010 at 20:18
3

I believe someone else said this first, but passing a reference to an anonymous object is illegal. Visual C++ is weird. To fix it, try something like this:

void class::a(const std::string &text)
{
    stdStringFromClass = text;
}

void class::b(const char *text)
{
    a(std::string(text));
}

BTW, I assume your class name is not actually 'class', and you're just using that as a placeholder, because

class class {
    // ...
};

stuff is illegal.

0

Without the actual error message I can only guess: class is a reserved keyword and you should name your class differently, this is not a valid method definition:

void class::a(std::string &text) { ... }

EDIT: the problem is in passing the anonymous reference, as others have pointed out. You can just ignore this answer now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.