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). 7 4693 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
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.
<im*****@hotmai l.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.
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.
imut...@hotmail .co.uk wrote:
Steve Pope wrote:
I don't get any errors on the above code using g++.
What version ? 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<ostrea m>. Try adding that to the top of
the code I posted.
Gavin Deane
<im*****@hotmai l.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 seen commercial programs
print output to the screen as well as to a log file.
depending on the user and other situations, i might want to turn off one of
the outputs or maybe even both outputs.
so, i want a single line with operator << function...
|
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 instance
for the copy constructor).
For example:
C++
vector<classvClass;
|
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 having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elEnd}'
|
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 the same functionality for
multimap. Since their interface is same for the problem at hand, I
|
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 of string, in one line. I could
| |
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |