473,508 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3810
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
7122
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
484
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
1645
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
1490
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
2487
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
2633
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
14892
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
1601
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
1390
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
1872
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
7123
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
7326
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
7383
jinu1996
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...
0
7498
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
5627
agi2029
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,...
1
5053
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...
0
4707
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...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.