![]() |
Operator overloading and inheritance with template classes.
Just thought of having a simple example which has everything you need to know about the basics of C++. It has Inheritance, polymorphism, operator overloading, templates using friend as well as non-friend functions.
Code: Cpp
|
Re: Operator overloading and inheritance with template classes.
Hi ,
I pasted the code given and when i am executing this it is not working .I am using a gcc 4.1.2 The error showed by the compller is In file included from /usr/include/c++/4.1.2/backward/iostream.h:31, from netoprinhtem.cpp:1: /usr/include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. netoprinhtem.cpp:33: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const Derived<T>&)’ declares a non-template function netoprinhtem.cpp:33: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning netoprinhtem.cpp:34: warning: friend declaration ‘std::istream& operator>>(std::istream&, Derived<T>&)’ declares a non-template function netoprinhtem.cpp:39: warning: friend declaration ‘Derived<T> operator+(T, Derived<T>)’ declares a non-template function netoprinhtem.cpp: In function ‘int main(int, char**)’: netoprinhtem.cpp:106: warning: converting to non-pointer type ‘double’ from NULL netoprinhtem.cpp: In member function ‘Derived<T> Derived<T>::operator+(T) const [with T = double]’: netoprinhtem.cpp:114: instantiated from here netoprinhtem.cpp:70: warning: converting to non-pointer type ‘double’ from NULL /tmp/ccFkepAp.o: In function `main': netoprinhtem.cpp:(.text+0xe5): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, Derived<double>&)' netoprinhtem.cpp:(.text+0xf8): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' netoprinhtem.cpp:(.text+0x41c): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' netoprinhtem.cpp:(.text+0x43d): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' netoprinhtem.cpp:(.text+0x45e): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' collect2: ld returned 1 exit status Please help me about this .Whenever i am using operator everloading in template class i am getting problems.. |
Re: Operator overloading and inheritance with template classes.
You should replace
Code:
#include<iostream.h>Code:
#include <iostream> |
Re: Operator overloading and inheritance with template classes.
Sir,
Thanks for replying to my post. But after replacing also the compliler shows errors. The error generate by the compiler is netoprinhtem.cpp:34: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const Derived<T>&)’ declares a non-template function netoprinhtem.cpp:34: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning netoprinhtem.cpp:35: warning: friend declaration ‘std::istream& operator>>(std::istream&, Derived<T>&)’ declares a non-template function netoprinhtem.cpp:40: warning: friend declaration ‘Derived<T> operator+(T, Derived<T>)’ declares a non-template function netoprinhtem.cpp: In function ‘int main(int, char**)’: netoprinhtem.cpp:107: warning: converting to non-pointer type ‘double’ from NULL netoprinhtem.cpp: In member function ‘Derived<T> Derived<T>::operator+(T) const [with T = double]’: netoprinhtem.cpp:115: instantiated from here netoprinhtem.cpp:71: warning: converting to non-pointer type ‘double’ from NULL /tmp/ccMrOSpk.o: In function `main': netoprinhtem.cpp:(.text+0xe5): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, Derived<double>&)' netoprinhtem.cpp:(.text+0xf8): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' netoprinhtem.cpp:(.text+0x41c): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' netoprinhtem.cpp:(.text+0x43d): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' netoprinhtem.cpp:(.text+0x45e): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Derived<double> const&)' If u can plese help me on this. Thankin you |
Re: Operator overloading and inheritance with template classes.
I think you have done some errors on your side. Try redoing it. I have edited the code to have the
Code:
using namespace std |
Re: Operator overloading and inheritance with template classes.
Your are facing a classic C++ problem.
Replace following function declaration: friend ostream& operator << (ostream&,const Derived<T>&); to friend ostream& operator << <> (ostream&,const Derived<T>&); and also replace friend istream& operator >> (istream&,Derived<T>&); to friend istream& operator >> <>(istream&,Derived<T>&); Note: Only make change in function declaration and not in function definition. The problem is that you need to tell the compiler that the declared friend function is a template. Let me know if this works or if you face any problem. |
Re: Operator overloading and inheritance with template classes.
Also note that this is a link time error not compile time:
Original code should compile successfully using -c command: gcc -c code.cpp |
Re: Operator overloading and inheritance with template classes.
Hi,
I am getting the following errors after applying the <> after the operator<< and operator>> as specified in a post above. Base.h:34: error: template-id 'operator<< <>' for 'std::basic_ostream<char, std::char_traits<char> >& operator<<(std::basic_ostream<char, std::char_traits<char> >&, const Derived<double>&)' does not match any template declaration Base.h:35: error: template-id 'operator>><>' for 'std::basic_istream<char, std::char_traits<char> >& operator>>(std::basic_istream<char, std::char_traits<char> >&, Derived<double>&)' does not match any template declaration Base.h: In function 'int main(int, char**)' Could someone please help? Thanks |
Re: Operator overloading and inheritance with template classes.
Quote:
|
Re: Operator overloading and inheritance with template classes.
This reply may be somewhat late, but as I was facing a similar problem just now and found this discussion, let me add one important remark that anybody reading the above will find helpful.
rockoder was right about the "<>" after the name of the function being necessary. The fact that shabbir is able to compile his code without adding it may indicate that he is using a compiler that is not very strict. But for example when using gcc 4.1, the "<>" is absolutely necessary to avoid the warnings and with ldd 2.7 it is necessary to avoid the linker errors. Now the reason why jijojs was still facing errors after making the correction is that a function (regardless of whether it is a template function or ordinary function) can never be declared "friend" of a class if its prototype wasn't declared before. So before the class definition of "Derived", you need the function prototype for the "operator<<" template. Moreover, because the operator<< takes an argument of type "Derived<T>", you must insert a forward-declaration of the class template, too. In the code this will look as follows: Code:
template <typename T> class Derived;http://cboard.cprogramming.com/showthread.php?t=86607 for a discussion of a similar situation and some alternative ideas. Another interesting problem arises when the class is defined in a special namespace, i.e. enclosed in something like this: Code:
namespace special { |
| All times are GMT +5.5. The time now is 12:03. |