1

Visual Studio Version 16.10.3, C++17, Win10-64.

I can't figure this one out. I think I've got all the buzzwords correct but I get errors for the following code:

#include <string>
using namespace std;
    
class AssetID {
private:
    string&           asset_Name;
public:
    const string& getName()  { return  asset_Name; }
}; // class AssetID
    
static
bool compareName(const AssetID* lhs, const AssetID* rhs) {
    return lhs->getName() < rhs->getName();
}; 
Code    Description Project File    Line    Suppression State
C2662   'const std::string &AssetID::getName(void)': cannot convert 'this' pointer from 'const AssetID' to 'AssetID &'  HOAAnalysis DTest.h 15  
C2662   'const std::string &AssetID::getName(void)': cannot convert 'this' pointer from 'const AssetID' to 'AssetID &'  HOAAnalysis Test.h  15  
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
lostbits
  • 928
  • 1
  • 9
  • 23
  • 1
    const string& getName() const{} – Michał Marszałek Jul 10 '21 at 21:10
  • I don't know why you need to return references but string& in class as member is somewhat wrong given your c++ level. – Michał Marszałek Jul 10 '21 at 21:12
  • What @MichałMarszałek said. There is rarely a good reason to use class member reference variables. There are consequences to this like losing the ability to copy a class object. See this for a list of issues: https://stackoverflow.com/questions/12387239/reference-member-variables-as-class-members – doug Jul 10 '21 at 22:29

1 Answers1

5
const AssetID* lhs

This is a pointer to a const object. Derefencing it gives you a const object. When you have a const object you can only call its const methods.

const string& getName() { return  asset_Name; }

This is not a const method. It is a method that returns a reference to a const object, which is not the same thing. This should be:

const string& getName() const { return  asset_Name; }

And now you have a const class method.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • 2
    The `string& asset_Name;` itself may be worth mentioning. Having a reference member variable is _often_ a mistake and I'm going to assume it is in this case. – Ted Lyngmo Jul 10 '21 at 21:14
  • 1
    @TedLyngmo the use of a reference member is certainly odd in this example, but it is not relevant to the actual issue at hand. Returning a reference to a non-reference member would still produce the same error, which has nothing to do with the member itself. – Remy Lebeau Jul 10 '21 at 21:18
  • @RemyLebeau 100% agreement. I was thnking of mentioning it as a note just to help out. – Ted Lyngmo Jul 10 '21 at 21:20
  • @Sam Varshavchik Thanks. The the rest, thanks. You have instructed me and I cam grateful. To the issue of having a member reference variable, well, it's not a variable and rather than look around for another mechanism, I chose this one. Dealer's choice, and has pointed out, the choice (advisable or not) has little to do with the issue. Thanks all. – lostbits Jul 10 '21 at 22:47
  • 1
    @lostbits If you need a reference to a variable owned "elsewhere" you could go for a pointer. That'd make `AssetID` objects move and copyable out of the box – Ted Lyngmo Jul 10 '21 at 22:56