0

I want to ask a question about friends of a class in C++.

I am a beginner in C++ and learning about overloading operators as global functions.

I wrote the following part of a class declaration in the Mystrings.h file and the corresponding function in the Mystrings.cpp file.

for Mystrings.h :

class Mystring
{
    friend bool operator==(const Mystring &lhs, const Mystring &rhs);
    friend Mystring operator-(const Mystring &obj);
    friend Mystring operator+(const Mystring &lhs, const Mystring &rhs);
private:
    char *str; // pointer to a char[] that holds a c-style string

and for Mystrings.cpp:

Mystring operator-(Mystring &obj) {
char *buff = new char[std::strlen(obj.str)+1];
std::strcpy(buff, obj.str);
for (size_t i = 0; i < std::strlen(buff); i++)
    buff[i] = std::tolower(buff[i]);
Mystring temp {buff};
delete [] buff;
return temp;
}

// concatenation
Mystring operator+(const Mystring &lhs, const Mystring &rhs) {
    char *buff = new char [std::strlen(lhs.str) + std::strlen(rhs.str) + 1];
    std::strcpy(buff, lhs.str);
    std::strcat(buff, rhs.str);
    Mystring temp {buff};
    delete [] buff;
    return temp;
}

For my main CPP file I was trying to make the following work:

Mystring three_stooges = moe + " " + larry + " " + "Curly";
three_stooges.display(); // Moe Larry Curly

However, the compiler returns an error:

error: 'str' is a private member of 'Mystring' 

for the lines

char *buff = new char[std::strlen(obj.str)+1];
std::strcpy(buff, obj.str);

I can't seem to see why.

I know that as I am declaring the friends of the function, they are now able to access the private string pointer, *str yet the error still persists. The concatenation operator + functions as normal but I can't work out why the error above persists.

Why is this error being yielded?

bodn19888
  • 167
  • 10
  • Please provide a [mre]. – Yunnosch Aug 13 '20 at 17:47
  • The friend has a `const`, did you notice? `Mystring operator-(Mystring &obj)` lacks it. – Yunnosch Aug 13 '20 at 17:49
  • 1
    Signature difference `friend Mystring operator-(const Mystring &obj);` vs `Mystring operator-(Mystring &obj)` – Richard Critten Aug 13 '20 at 17:49
  • 1
    The hacker deep in me loves that you are allocating and freeing memory based on strlen() , then using strcpy() - but that is a much more advanced lesson :) – Michael Dorgan Aug 13 '20 at 17:51
  • @RichardCritten out of interest, how does the omission of `const` result in the fact that it can't access `str` in the first place? – bodn19888 Aug 13 '20 at 17:53
  • `Mystring` could probably benefit from having and using a `size` member to track the length of the stored string. – user4581301 Aug 13 '20 at 17:53
  • @MichaelDorgan it was covered in my course I genuinely had no idea it was an advanced topic until I saw it discussed here on SO but glad to know my course set me up nicely! – bodn19888 Aug 13 '20 at 17:54
  • If the function signatures don't match, they are not the same function. Effectively `Mystring` is Bob's friend, not Alice's. – user4581301 Aug 13 '20 at 17:54
  • 1
    @bodn19888 `Mystring operator-(Mystring &obj)` is not a member function or a friend function - it's just a top level function with no special access rights. – Richard Critten Aug 13 '20 at 17:55
  • @RichardCritten of course - it has no relation to the friend function prototype I defined in the class! Understood clearly! – bodn19888 Aug 13 '20 at 17:57

1 Answers1

1

Simple error:

The function prototype for unary minus operator- contains a const but this was omitted in the Mystrings.cpp file.

bodn19888
  • 167
  • 10
  • The problem isn't in the concatenation (`operator+`), but in the unary minus (`operator-`). Although the posted code doesn't use that operator, its definition is where the error is being reported. It's the only code that refers to `obj.str`. If you clarify that, you've got a good answer. – Pete Becker Aug 13 '20 at 19:26
  • @PeteBecker Done! – bodn19888 Aug 13 '20 at 19:53