473,379 Members | 1,544 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,379 software developers and data experts.

operator<< for templated subclass?

Hi template specialists,

I have a problem with the code listed below.

What I wanted to do is defining an operator<< that is able to output
a 'matrix<ELEMENT_T, INDEX_T>::subrange' object
into a 'std::basic_ostream<charT, traits>'.

For some reason, it does not work. The compiler always complains:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
te.cpp:
Error E2094 test.cpp: 'operator<<' not implemented in type
'std::ostream' for arguments of type 'matrix<double,unsigned
int>::subrange' in function main(int,char * *)
*** 1 errors in Compile ***

The INTEL compiler gives a similar error.

On the other hand, MSVC 7 compiles without any warnings.

So - which of the compilers are correct?

And does anyone have an idea what there might be wrong with the code?

Note:

If I change the code to output a
'matrix<ELEMENT_T, INDEX_T>'
instead of a
'matrix<ELEMENT_T, INDEX_T>::subrange'
then it works perfectly for all compilers!

Only that this is not what I actually want to do.

So, please help me if you possibly can!

---------snip-----------
#include <iostream>
#include <cstdlib>
template<typename ELEMENT_T, typename INDEX_T>
struct matrix {
// I want to output an object of this nested type.
struct subrange {};
};

// This templated global function will not be used
// due to some reason unknown to me.
template<
typename ELEMENT_T, typename INDEX_T, typename charT, typename traits

std::basic_ostream<charT, traits> &operator<<(
std::basic_ostream<charT, traits> &os,
matrix<ELEMENT_T, INDEX_T>::subrange const &range)
{
os <<"matrix range";
return os;
}

int main(int, char **) {
matrix<double, size_t>::subrange x;
std::cout << x;
return EXIT_SUCCESS;
}
---------snip-----------

sincerly,

Guenther

Jul 19 '05 #1
4 3801
Guenther Brunthaler wrote:
std::basic_ostream<charT, traits> &operator<<(
std::basic_ostream<charT, traits> &os,
matrix<ELEMENT_T, INDEX_T>::subrange const &range)


typename matrix<ELEMENT_T, INDEX_T>::subrange const &range)

http://www.cuj.com/documents/s=8464/cuj0308dewhurst/

--
Attila aka WW
Jul 19 '05 #2
On Tue, 30 Sep 2003 10:03:42 GMT, Guenther Brunthaler
<no********************@yahoo.com> wrote:
Hi template specialists,

I have a problem with the code listed below.

What I wanted to do is defining an operator<< that is able to output
a 'matrix<ELEMENT_T, INDEX_T>::subrange' object
into a 'std::basic_ostream<charT, traits>'.

For some reason, it does not work. The compiler always complains:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
te.cpp:
Error E2094 test.cpp: 'operator<<' not implemented in type
'std::ostream' for arguments of type 'matrix<double,unsigned
int>::subrange' in function main(int,char * *)
*** 1 errors in Compile ***

The INTEL compiler gives a similar error.

On the other hand, MSVC 7 compiles without any warnings.

So - which of the compilers are correct?
INTEL and Borland. matrix<ELEMENT_T, INDEX_T>::subrange is a
nondeduced context since it requires template argument deduction of
the template parameters of a parent type based on the type of a nested
type. The compiler can't perform this deduction in general, because
the mapping of subtype onto template parameter types is not in general
many to one, but many to many.
And does anyone have an idea what there might be wrong with the code?

Note:

If I change the code to output a
'matrix<ELEMENT_T, INDEX_T>'
instead of a
'matrix<ELEMENT_T, INDEX_T>::subrange'
then it works perfectly for all compilers!
Right, since that doesn't require argument deduction from a nested
type.
Only that this is not what I actually want to do.

So, please help me if you possibly can!


This version works:

#include <iostream>
#include <cstdlib>

template<typename ELEMENT_T, typename INDEX_T>
struct matrix {
// I want to output an object of this nested type.
struct subrange
{
template<typename charT, typename traits>
friend std::basic_ostream<charT, traits> &operator<<(
std::basic_ostream<charT, traits> &os,
subrange const &range)
{
os <<"matrix range";
return os;
}
};
};

int main(int, char **) {
matrix<double, size_t>::subrange x;
std::cout << x;
return EXIT_SUCCESS;
}

http://www.parashift.com/c++-faq-lit...html#faq-34.15
is the inverse of what I've done above - the linker errors have been
avoided by putting the definition inline. The template argument
deduction problem has been avoided by making removing the ELEMENT_T
and INDEX_T template parameters from operator<< so that it doesn't
have to deduce them.

When matrix<double, size_t>::subrange is instantiated, the friend
function will also be injected into the global namespace, hence koenig
lookup on the operator<< call finds the template and instantiates and
calls it for <char, char_traits<char> >.

Tom
Jul 19 '05 #3
Thanks a lot, Tom and Attila!

tom_usenet wrote:
This version works:


But unfortunately not for C++ Builder. Nor for MSVC. Nor GCC.

But at least it works for INTEL.

Nevertheless, thanks to you now I know at least what the problem was!

The fact that so many C++ Compiler still do not comply with the C++
standard is a different story anyway ;-)
sincerly,

Guenther

(Note: If you want to reply via e-mail, please remove the
substrings "nospam_" and "_dontspam" from my address.)

Jul 19 '05 #4
On Wed, 01 Oct 2003 03:57:08 GMT, Guenther Brunthaler
<no********************@yahoo.com> wrote:
Thanks a lot, Tom and Attila!

tom_usenet wrote:
This version works:


But unfortunately not for C++ Builder. Nor for MSVC. Nor GCC.

But at least it works for INTEL.

Nevertheless, thanks to you now I know at least what the problem was!

The fact that so many C++ Compiler still do not comply with the C++
standard is a different story anyway ;-)


GCC 3.2 is fine with it, as is MSVC7.1 (2003). Time to upgrade!

Tom
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
0
by: Guenther Brunthaler | last post by:
Hi template specialists, I have a problem with the code listed below. What I wanted to do is defining an operator<< that is able to output a 'matrix<ELEMENT_T, INDEX_T>::subrange' object into...
1
by: Jacob Foshee | last post by:
Greetings, Using MS Visual C++ .NET 2003, I have the following: template <class T> class vector3 { // ... friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream operator }:
1
by: Eddie Parker | last post by:
Hi! I'm having an interesting problem that I can't seem to get to work, and I'm curious if someone could either a) tell me how to make it work, or b) tell me why it *can't* work. :) Anyhow,...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
4
by: homsan toft | last post by:
I've tried the below code with MSVC and Comeau online compiler. Both complain that operator<< for Outer<part<size_t> >::inner is not defined. So how do I declare it without doing full...
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...
6
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
1
by: Stuart Golodetz | last post by:
Hi guys, I'm trying to making an instance of a templated operator<< for a templated class a friend of that class (see below), to allow it to access the class internals for output purposes. ...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.