how to implement << ala std::cout for template of std::vector ?

I

imutate

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).
 
G

Gavin Deane

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-lite/how-to-post.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
 
I

imutate

Gavin said:
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-lite/how-to-post.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

Interesting, I don't have Comeau but I might get it. I am using g++ on
linux I wonder if xcode will give the same error which is

no match for operator << in std::eek:perator..

and it continues for several pages mentioning "with _Traits" alot.

I can paste more details, but my linux box is not online so I have to
transfer the text.
 
S

Steve Pope

Gavin Deane wrote:
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); }
};
Interesting, I don't have Comeau but I might get it. I am using g++ on
linux I wonder if xcode will give the same error which is

no match for operator << in std::eek:perator..

I don't get any errors on the above code using g++.

S.
 
G

Gavin Deane

Gavin said:
How do I implement << ala std::cout for vector template ?

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-lite/how-to-post.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

Interesting, I don't have Comeau but I might get it. I am using g++ on
linux I wonder if xcode will give the same error which is

no match for operator << in std::eek:perator..

Without the complete error message I can only guess, and the only guess
I can think of is to #include<ostream>. Try adding that to the top of
the code I posted.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top