I have a parent class and some that derive from it. I want to have a function that will mimic the java's equivalent of .toString()
I searched online and found out that I can overload the << operator which I did, but I'm getting an error:
multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Word&)'
I get the same error for HTMLElement
I'm including two of the files I'm using to test my new findings. Here they are:
@firedraco I did what you suggested and I get an error
expected constructor, destructor, or type conversion before ‘&’ token
I tried without the definition in the .h having the whole
ostream& operator<<
method in my .cpp files and then I get another error
no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"\012first: ")) << llhe.LinkedList<TYPE>::GetFirst [with TYPE = HTMLElement]()->LLNode<TYPE>::GetValue [with TYPE = HTMLElement]()’
and I get this last same error several times... any idea on how to fix the whole thing?
@olredixsis Just curious, why is it not a good idea to use
Make sure you are including the files you need for ostream in the .h file.
h4x0rmx wrote:
Just curious, why is it not a good idea to use
using namespace std;
on the .h files?
It forces others who include you header to have namespace std included, which could cause problems if they having functions named the same or something similar.
As for namespaces in .cpp files, you can do it and it won't cause any problems. However it is generally a good idea to avoid using using and instead just type out the std::. If it is something large an obnoxious (like my_class::sub_type::info_type::data::static), then it you should try to limit the using only to the scope where it is needed.
inc/HTMLElement.h:44: error: ISO C++ forbids declaration of ‘ostream’ with no type
inc/HTMLElement.h:44: error: ‘ostream’ is neither function nor member function; cannot be declared friend
inc/HTMLElement.h:44: error: expected ‘;’ before ‘&’ token
inc/HTMLElement.h:56: error: expected constructor, destructor, or type conversion before ‘&’ token
src/HTMLElement.cpp: In function ‘std::ostream& operator<<(std::ostream&, const HTMLElement&)’:
src/HTMLElement.cpp:28: error: passing ‘const HTMLElement’ as ‘this’ argument of ‘virtual int HTMLElement::getType()’ discards qualifiers
src/HTMLElement.cpp:28: error: passing ‘const HTMLElement’ as ‘this’ argument of ‘virtual std::string HTMLElement::getValue()’ discards qualifiers
@olredixsis I'm sorry, but I have to ask... with this new code, how am I supposed to implement an HTMLElement? How about the classes that derive from it? What are you making a template?
How would I instantiate a Word element? Do I have to specify a <T> type?
@olredixsis That works good enough, except for the fact that when I have a instantiate a Word element, the HTMLElement's << operator gets called.
i.e. I have a LinkedList<HTMLElement>, I call
htmlElementLinkedList.GetFirst()->GetValue()
which returns a Word object, and my output is
HTMLElement(1 a)
Do I have to do some casting or should it know what method/function to call?
Thanks!
Edited:
I just noticed that when I have a Word object by itself it knows what << function to call, but not so when my Word is inside a LinkedList<HTMLElement>