By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

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

P: n/a
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).

Oct 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

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

Oct 2 '06 #2

P: n/a

Gavin Deane wrote:
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
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::operator..

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.

Oct 2 '06 #3

P: n/a
<im*****@hotmail.co.ukwrote:

>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::operator..
I don't get any errors on the above code using g++.

S.
>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.

Oct 3 '06 #4

P: n/a

Steve Pope wrote:
I don't get any errors on the above code using g++.
I ran it on mac OSX and I get the same error.

Oct 3 '06 #5

P: n/a

imut...@hotmail.co.uk wrote:
Steve Pope wrote:
I don't get any errors on the above code using g++.
What version ?

Oct 3 '06 #6

P: n/a

im*****@hotmail.co.uk wrote:
Gavin Deane wrote:
im*****@hotmail.co.uk wrote:
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-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

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::operator..
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

Oct 3 '06 #7

P: n/a
<im*****@hotmail.co.ukwrote:
Steve Pope wrote:
I don't get any errors on the above code using g++.
>What version ?
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-112.7.2)

S.
Oct 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.