-1

I wanted to try to overload "<<" operator for an output stream and a custom class Article.

my.h

#include <iostream>
using namespace std;
class Article {
    string body;
    string header;
    string author;
public:
    Article();
    void addHeader(const string&);
    void addBody(const string&);
    string getHeader();
    string getBody();
};

my.cpp

string Article::getHeader() {
    return header;
}


void Article::addBody(const string& body) {
    this->body = body;
}
void Article::addHeader(const string& header) {
    this->header = header;
}

ostream& operator<<(ostream& os, Article& article) {
    return os << article.getHeader() << "\n\n" << article.getBody();
}

main.cpp

#include <iostream>
#include <string>
#include "my.h"
void main() {
    char bye;
    Article article = Article();
    article.addBody("This is my article! thank you!");
    article.addHeader("Header");
    cout << article.getHeader() << "\n";
    cout << article.getBody() << "\n";
    cout << article;
    cin >> bye;
}

This code doesn't compile. VS 2013 says:

binary '<<' : no operator found which takes a right-hand operand of type 'Article' (or there is no acceptable conversion)

If I remove the last line, it compiles successfully and the output of getHeader() and getBody() is as expected. They both return strings so it should be a piece of cake.

This problem seems very simple, however, as simple as it is I can't figure out what is happenning.

webdive
  • 87
  • 9
  • 3
    Your example has other problems: `Article article = new Article();` attempts to assign an `Article *` to an `Article`. If you post the real code we might be able to help you. – Alan Stokes Sep 19 '15 at 15:35
  • oh, yeah, that's a mistype, sorry. For some reason I added "new" here. – webdive Sep 19 '15 at 15:41
  • 1
    It's not just the `new`. Just write `Article article;`. That's it. No need for complication. – Lightness Races in Orbit Sep 19 '15 at 15:42
  • 1
    There is not enough to go on here. Present your [minimal testcase](http://stackoverflow.com/help/mcve). The `operator<<` overload isn't being recognised from your scope and we can't tell you why from here. Instead posting some hand-crafted, modified version of the broken code, which inevitably contains typos unrelated to the problem (case in point) just wastes everybody's time. Construct a minimal testcase that you _know_ reproduces the problem, then post that, verbatim, without fiddling. – Lightness Races in Orbit Sep 19 '15 at 15:43
  • 1
    Don't type your code in, just copy and paste. – Beta Sep 19 '15 at 15:43
  • Added full source code – webdive Sep 19 '15 at 15:47
  • What happens when you remove the incorrect parentheses, as @LightnessRacesinOrbit suggested? As written, you're trying to set an `Article` equal to a member function; doesn't your compiler complain about that? – Beta Sep 19 '15 at 15:49
  • 2
    @Beta: You are mistaken. `Article article = Article()` is not _incorrect_ (unless `Article` is non- copyable); it's just pointless. There is nothing to do with member functions here. – Lightness Races in Orbit Sep 19 '15 at 15:52
  • 1
    @webdive: Who told you to "add full source code"? Post your _minimal testcase_. I linked you to the page that explains how to write one. – Lightness Races in Orbit Sep 19 '15 at 15:52
  • By the way, `void main` is not valid C++. It must be `int main`. – Christian Hackl Sep 19 '15 at 15:53
  • @LightnessRacesinOrbit: I stand corrected. – Beta Sep 19 '15 at 16:25

1 Answers1

3

Now that you have posted real code the answer is obvious.

You define your operator << in my.cpp, but you don't declare it in my.h, so when compiling main.cpp the compiler has no way of knowing it exists.

Add a declaration of the function to your header.

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64