fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class MyClass
  9. {
  10. string& Str;
  11.  
  12. public:
  13. MyClass(string& str) : Str(str)
  14. {
  15.  
  16. };
  17.  
  18.  
  19. void SetString(string& str)
  20. {
  21. Str = str;
  22. }
  23. string GetString() const
  24. {
  25. return Str;
  26. }
  27. };
  28.  
  29. void fn(const MyClass& myClassInstance)
  30. {
  31. string str2{"str2"};
  32. myClassInstance.SetString(str2);
  33. }
  34.  
  35. int main() {
  36. string str{"str1"};
  37. MyClass myObj{str};
  38. cout << myObj.GetString() << endl;
  39. fn(myObj);
  40. cout << myObj.GetString() << endl;
  41. return 0;
  42. }
  43.  
Compilation error #stdin compilation error #stdout 0s 4448KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void fn(const MyClass&)’:
prog.cpp:32:35: error: passing ‘const MyClass’ as ‘this’ argument discards qualifiers [-fpermissive]
     myClassInstance.SetString(str2);
                                   ^
prog.cpp:19:10: note:   in call to ‘void MyClass::SetString(std::__cxx11::string&)’
     void SetString(string& str)
          ^~~~~~~~~
stdout
Standard output is empty