im*****@hotmail.co.uk wrote:
How do I implement << ala std::cout for vector template ?
I already have the following:
#include <vector>
template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
};
I want to output at(i) in the above
T& operator[](int i) {
std::cout << "[" << i << "]=" << this -at(i) << std::endl;
return this -at(i); }
But the compiler does not like this -at(i).
In what way does the compiler "not like" it. What error message do you
get? FAQ 5.8 explains how to post questions about code that does not
work correctly. Follow the advice there otherwise people might not be
able to help.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Comeau online compiles the following no problem
#include <vector>
#include <iostream>
template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
const T& operator[](int i) const { return this -at(i); }
T& operator[](int i) {
std::cout << "[" << i << "]=" << this -at(i) << std::endl;
return this -at(i); }
};
Gavin Deane