OK. Most of those are spurious, unreal errors. The compiler gets
confused due to the real technical errors.
With the /language/ errors fixed it compiles:
---------------------------------------------------------------------
#include <iostream>
#include <typeinfo>
#include <cstring>
using std::string;
using std::ostream;
template <class T>
class MyClass
{
T *data;
public:
MyClass(T);
MyClass(const MyClass<T> &);
~MyClass();
template< class U > //!
friend ostream & operator<<(ostream &, const MyClass<U> &); //!
};
template <class T>
MyClass<T>::MyClass(T val)
{
data = new T;
*data = val; //!
}
template <class T>
MyClass<T>::MyClass(const MyClass<T> &obj)
{
data = new T;
if( typeid(T) == typeid(char *) )
{
int len = strlen(obj.data);
strncpy(data, obj.data, len+1);
}
else
{
data = obj.data;
}
}
template <class T>
MyClass<T>::~MyClass()
{
delete data;
}
template <class T>
ostream &operator<<(ostream &s, const MyClass<T> &obj)
{
return s << obj.data;
}
int main()
{
MyClass<char const*> test("Hello guys!"); //!
using namespace std;
cout << test << endl;
}
---------------------------------------------------------------------
Note that there are still a host of errors in this code, but the
compiler I used, with the options I used, wasn't smart enough to
diagnose them, and isn't required by the standard to diagnose them.
As general advice, instead of dealing with `char const*` and `new` etc.,
just use `std::string` and `std::vector`.
More comfortable, more safe, more easy, can even be faster. ;-)
Cheers!,
- Alf