Having some trouble understanding how to overload operators (specifically << and >> ) for
a templated class.
Here's what I have in Attribute.h:
Code:
#include <iostream>
template <typename T>
class Primitive
{
template<typename C> friend istream& operator>>(istream& in, Primitive<C>& p) { in >> p.data; return in; }
template<typename C> friend ostream& operator<<(ostream& out, const Primitive<C>& p) { out << p.data; return out; }
public:
Primitive(T d) { data=d; }
Primitive(Primitive& p) { data=p.getData(); }
~Primitive(){};
T getData(){ return data; };
private:
T data;
};
This is the main.cpp I use:
Code:
#include <cstdlib>
#include <iostream>
#include <Attribute.h>
int main(int argc, char *argv[])
{
Attribute<int> aI(5);
Attribute<string> aS("Bob");
cout<<aI;
system("PAUSE");
}
and these are the errors:
Code:
In file included from main.cpp:8:
I:/Dev-Cpp/include/c++/3.4.2/Primitive.h:7: error: expected init-declarator before '&' token
I:/Dev-Cpp/include/c++/3.4.2/Primitive.h:7: error: expected `;' before '&' token
I:/Dev-Cpp/include/c++/3.4.2/Primitive.h:8: error: expected init-declarator before '&' token
I:/Dev-Cpp/include/c++/3.4.2/Primitive.h:8: error: expected `;' before '&' token
I feel I may be a little over my head as I just got the hang of template classes and functions. I've read the few posts here that I could find with my meager script-fu, but I'm coming up short on understanding. Help?