S
subramanian100in
Consider the foillowing program x.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class TemplateClass
{
public:
explicit TemplateClass(const T& arg = T());
ostream& print(ostream& os) const;
private:
T t;
};
template<typename T>
inline TemplateClass<T>::TemplateClass(const T& arg) : t(arg)
{
}
template <typename T>
inline ostream& TemplateClass<T>:rint(ostream& os) const
{
return os << "from TemplateClass<T>:rint(): " << t;
}
template <typename T>
inline ostream& operator<<(ostream& os, const T& obj)
{
return obj.print(os);
}
int main()
{
string str("Test string");
TemplateClass<string> obj(str);
cout << obj << endl;
return EXIT_SUCCESS;
}
When I compile this program as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get the following errors:
x.cpp: In member function `std:stream&
TemplateClass<T>:rint(std:stream&) const [with T = std::string]':
x.cpp:32: instantiated from `std:stream& operator<<(std:stream&,
const T&) [with T = TemplateClass<std::string>]'
x.cpp:40: instantiated from here
x.cpp:26: error: ambiguous overload for 'operator<<' in 'os << "from
TemplateClass<T>:rint(): "'
x.cpp:31: note: std:stream&
operator<<(std:stream&, const T&) [with T = char[33]]
These errors occur because I have made the 'operator<<' a
function template. Is it correct ?
Is it possible to get rid of these compilation errors while keeping
the 'operator<<' a function template ? If so kindly explain with
program sample. If not possible, then we have to write
inline ostream& operator<<(ostream& os,
const SomeSpecificType& obj)
where 'SomeSpecificType' is a explicit class name.
Is this understanding of mine is correct ?
Thanks
V.Subramanian
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class TemplateClass
{
public:
explicit TemplateClass(const T& arg = T());
ostream& print(ostream& os) const;
private:
T t;
};
template<typename T>
inline TemplateClass<T>::TemplateClass(const T& arg) : t(arg)
{
}
template <typename T>
inline ostream& TemplateClass<T>:rint(ostream& os) const
{
return os << "from TemplateClass<T>:rint(): " << t;
}
template <typename T>
inline ostream& operator<<(ostream& os, const T& obj)
{
return obj.print(os);
}
int main()
{
string str("Test string");
TemplateClass<string> obj(str);
cout << obj << endl;
return EXIT_SUCCESS;
}
When I compile this program as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get the following errors:
x.cpp: In member function `std:stream&
TemplateClass<T>:rint(std:stream&) const [with T = std::string]':
x.cpp:32: instantiated from `std:stream& operator<<(std:stream&,
const T&) [with T = TemplateClass<std::string>]'
x.cpp:40: instantiated from here
x.cpp:26: error: ambiguous overload for 'operator<<' in 'os << "from
TemplateClass<T>:rint(): "'
x.cpp:31: note: std:stream&
operator<<(std:stream&, const T&) [with T = char[33]]
These errors occur because I have made the 'operator<<' a
function template. Is it correct ?
Is it possible to get rid of these compilation errors while keeping
the 'operator<<' a function template ? If so kindly explain with
program sample. If not possible, then we have to write
inline ostream& operator<<(ostream& os,
const SomeSpecificType& obj)
where 'SomeSpecificType' is a explicit class name.
Is this understanding of mine is correct ?
Thanks
V.Subramanian