Hi I am trying to overload >> operator for custom templated vector class but getting error Undifined Type 'T'
-
istream& operator>>(istream& cin, Vector& v)
{cout << "input size" << endl; int n ; cin >> n; cout << "intput vector" << endl; for (int i = 0; i < n; i++) { T data; cin >> data; v.push\_back(data); } return cin;
}
int main()
{
Vector v(5);cin>>v;
}
Getting error undefined type T data; -
istream& operator>>(istream& cin, Vector& v)
{cout << "input size" << endl; int n ; cin >> n; cout << "intput vector" << endl; for (int i = 0; i < n; i++) { T data; cin >> data; v.push\_back(data); } return cin;
}
int main()
{
Vector v(5);cin>>v;
}
Getting error undefined type T data;If your intention is to use
std::vector
, your problem is that it shouldn't be capitalized.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
istream& operator>>(istream& cin, Vector& v)
{cout << "input size" << endl; int n ; cin >> n; cout << "intput vector" << endl; for (int i = 0; i < n; i++) { T data; cin >> data; v.push\_back(data); } return cin;
}
int main()
{
Vector v(5);cin>>v;
}
Getting error undefined type T data;Either you make your function a template:
template istream& operator>>(istream& cin, vector& v)
{
...or you make it a specific function for vectors of strings:
istream& operator>>(istream& cin, vector& v)
{cout << "input size" << endl; int n; cin >> n; cout << "intput vector" << endl; for (int i = 0; i < n; i++) { string data; cin >> data; v.push\_back (data); } return cin;
}
Either solution works. (that's beside Greg's observation about capitalization.)
Mircea
-
If your intention is to use
std::vector
, your problem is that it shouldn't be capitalized.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Either you make your function a template:
template istream& operator>>(istream& cin, vector& v)
{
...or you make it a specific function for vectors of strings:
istream& operator>>(istream& cin, vector& v)
{cout << "input size" << endl; int n; cin >> n; cout << "intput vector" << endl; for (int i = 0; i < n; i++) { string data; cin >> data; v.push\_back (data); } return cin;
}
Either solution works. (that's beside Greg's observation about capitalization.)
Mircea
-
Then see Mircea's first solution below, but keep
Vector
capitalized.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
istream& operator>>(istream& cin, Vector& v)
{cout << "input size" << endl; int n ; cin >> n; cout << "intput vector" << endl; for (int i = 0; i < n; i++) { T data; cin >> data; v.push\_back(data); } return cin;
}
int main()
{
Vector v(5);cin>>v;
}
Getting error undefined type T data;When using already existing container types from the STL, just use the container type as template parameter. You can retrieve the element type with the internal type definition
value_type
like this:template
istream& operator>>(istream& my_istream, Container& v)
{
typedef typename Container::value_type T; // now you can use T for your element type
...If you define your own container type, you can do it in the same way. You just have to make sure your container type does define
value_type
:template
class MyContainer {
public:
typedef Element value_type;
...P.S.: IIRC STL containers already do have overrides for operator>>. You could save yourself some effort by just overriding this operator for your base type:
template
istream& operator>>(istream& my_istream, T& value) {
...It should work just as well as the above solution. P.P.S.: As it turns out I was wrong. There are no default implementations of operator>> for containers.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)