473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
7 4667

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

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

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

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

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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
7
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm...
10
by: ozizus | last post by:
I overloaded operator << for STL map successfully: template <typename T1, typename T2ostream & operator << (ostream & o, map <T1,T2& m) { //code } the code works like a charm. Now, I want...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.