Why does msvc let me do this, but not gcc / g ++?

In msvc, I have functions like this and it builds, but in gcc he doesn't like it.

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

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

The problem here is &, gcc. I think it is worrying that since I just created this std :: string, passing by reference is risky, so it is not created, but msvc does not even warn me.

Why is this incorrect C ++ for gcc I keep hearing that msvc is more strict than gcc.

thank

Error

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

this will be good?

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

void classname::b(char *text)
{
   std::string k = text;
    a(k);
}
+3
source share
4 answers

I have no idea why Visual Studio allows you to compile this, but you cannot pass a reference to an anonymous object.

: ok const, , . rvalue ++ 0x.

Edit2: , , a(), , , .

+3

, , Visual Studio , Visual ++ . ( /Za) , .

/W4 , :

C4239:

GCC const:

: 'std::string & 'std::string

+6

, - , . Visual ++ . , - :

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

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

, , "", ,

class class {
    // ...
};

.

+3

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

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

EDIT: The problem is passing an anonymous link, as others have pointed out. You can simply ignore this answer.

0
source

Source: https://habr.com/ru/post/1768778/


All Articles