0

So, I have recently approched OOP in c++ through university and I found myself with a few problems. I gave a shot at overloading the ostream operator << and found myself with some problems to solve. First was that doing the overloading as ostream& operator<<(ostream& outs, const Test&); carries a problem described as \src\Test.h:15:48: error: 'std::ostream& test::Test::operator<<(std::ostream&, const test::Test&)' must take exactly one argument ostream& operator<<(ostream& outs, const Test&);, so, just like I did with the operator == removed the second argument. But once i Try to build the following code i get the error: \src\main.cpp:9:10: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' cout << t;. I read this error but can't comprhend it, what should I do and what is the mistake I'm doing?

Test.h

#include <iostream>
#include <cstring>
using namespace std;
#ifndef SRC_TEST_H_BECCIO
#define SRC_TEST_H_BECCIO

namespace test {

class Test {
private:
    string mStr;
public:
    Test(string str);
    string getString(){return this->mStr;};
    ostream& operator<<(ostream& outs);
};

} /* namespace test */

#endif /* SRC_TEST_H_BECCIO */

Test.cpp

#include "Test.h"

namespace test {

Test::Test(string str) {
    this->mStr=str;

}

ostream& Test::operator<<(ostream& outs){

    outs << this->getString()<<endl;
    return outs;
}

} /* namespace test */

main.cpp

#include <iostream>
#include "Test.h"
using namespace std;
using namespace test;

int main() {

    Test t("let's hope this goes well");
    cout << t;
    return 0;
}
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
  • 2
    [What are the basic rules and idioms of operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). Specifically [here](https://stackoverflow.com/questions/4421706/operator-overloading-in-c/4421719#4421719). – WhozCraig Mar 24 '22 at 10:36

1 Answers1

0

It looks like the order of the arguments is backwards. See https://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes. Try this declaration, and a similar implementation:

friend ostream& operator<<(ostream& os, Test t);
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
  • Thanks for the help :), but now I've got some questions for you. 1.Why is it necessary to add the `friend` keyword? 2. How comes I should not put the `const` keyword before Test? And lastly 3. Can I write the implementation code for a friend function in the implementation file (Test.cpp)? Or do I need to do it in the same file as the declaration (Test.h)? – Beccio-san Mar 24 '22 at 11:11
  • @Beccio-san You're welcome! If I've answered the question, please see https://stackoverflow.com/help/someone-answers. 1. It allows the method to access the private members of `Test`. If you implement it with `getString` instead of `mStr` directly (be careful with the performance hit from extra copying!), you shouldn't need it. 2. Good point! It should probably be either `Test const` or `Test const&`! 3. I assume you should be able to write it in either location, as long as the header file has at least a declaration. See https://en.cppreference.com/w/cpp/language/friend for more about `friend`. – Solomon Ucko Mar 24 '22 at 11:48