Problems overloading the operator<< ( multiple definition)

Pages: 12
Hmm... I think you should try an upcasting since the Word class is a subclass of HTMLElement class. I'll will look your code later, I'm a bit busy right now.
Thanks! I'll wait for your answer!
What error(s) did you get?
Last edited on
I don't get any errors. It works as expected when I have single objects derived from HTMLElement, but when I have a LinkedList<HTMLElement>, only the HTMLElement versin of << gets called, ignoring the ones specific for the derived clases.
That's why I'm asking if I have to do some kind of casting... it would be cool to have the code figure out by itself what it function it should use.
did you revise your code for word.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef WORD_H_
#define WORD_H_

#include "HTMLElement.h"
#include "URL.h"
#include <string>
#include <iostream>

// Forward declare Word 
class Word;
// Prototype operator<<.

ostream& operator<<(ostream& ostrw, const Word& w);

class Word :public HTMLElement {
public:
	Word(std::string v, URL u);
	std::string &getURL();
	friend ostream& operator<<(ostream& ostrw, const Word& w);
private:
	URL url;
};
#endif 
that's exactly what I have :\
Try deleting the operator << for the subclass Word and use the operator << of base class HTMLElement.

word.h must inherit the operator << from its base class I think?

overloading operator can be inherited right?

correct me if I'm wrong. :)
Last edited on
The subclass should have its own operator <<, it should not inherit.
The subclasses have other variables/methods that I'd like to include. :)
If you are defining the operators the way you are (as global friends), then they will NOT be inherited.
I think I know what the problem is here... but let me make sure I have everything right.

1) You want to output stuff for HTMLElement and Word
2) You figured you could output via the << operator
3) You have a container of HTMLElement* (which may be of HTMLElements or Words or other derived types)
4) You want each type to print things in its own way.

Assuming I have all of this right so far....


This screams polymorphism. And unfortuantely you can't make the << operator virtual in this case. So instead, make a virtual Print function:

1
2
3
4
5
6
7
8
class HTMLElement:
{
public:
    virtual void Print(std::ostream& out) const
    {
        out << "whatever";
    }
    //... 


Put the same thing in Word. And instead of doing cout << *ptr; , you do ptr->Print(cout);


EDIT:

a nice perk to this approach is that you can overload << too, and make it fake virtual by having it call Print:

1
2
3
4
5
6
7
// you only have to write this for HTMLElement, and it will work as
//   desired for all derived types
std::ostream& operator << (std::ostream& out, const HTMLElement& el)
{
    el.Print(out);
    return out;
}
Last edited on
Right, the I'm using polymorphism... sorry if I wasn't clear enough.

I really appreciate your help!
Good for you. :)
Topic archived. No new replies allowed.
Pages: 12