473,660 Members | 2,475 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>::subra nge' object
into a 'std::basic_ost ream<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>::subra nge'
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<typena me 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_ostr eam<charT, traits> &operator<<(
std::basic_ostr eam<charT, traits> &os,
matrix<ELEMENT_ T, INDEX_T>::subra nge const &range)
{
os <<"matrix range";
return os;
}

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

sincerly,

Guenther

Jul 19 '05 #1
4 3826
Guenther Brunthaler wrote:
std::basic_ostr eam<charT, traits> &operator<<(
std::basic_ostr eam<charT, traits> &os,
matrix<ELEMENT_ T, INDEX_T>::subra nge const &range)


typename matrix<ELEMENT_ T, INDEX_T>::subra nge 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>::subra nge' object
into a 'std::basic_ost ream<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::ostrea m' 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>::subra nge 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<ELEMEN T_T, INDEX_T>'
instead of a
'matrix<ELEMEN T_T, INDEX_T>::subra nge'
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<typena me ELEMENT_T, typename INDEX_T>
struct matrix {
// I want to output an object of this nested type.
struct subrange
{
template<typena me charT, typename traits>
friend std::basic_ostr eam<charT, traits> &operator<<(
std::basic_ostr eam<charT, traits> &os,
subrange const &range)
{
os <<"matrix range";
return os;
}
};
};

int main(int, char **) {
matrix<double, size_t>::subran ge 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>::subran ge 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<cha r> >.

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
7155
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): <code:templates.h> #include <string> #include <iostream>
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 a 'std::basic_ostream<charT, traits>'. For some reason, it does not work. The compiler always complains:
1
1655
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
1499
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, here's a small test case that should compile on most compilers: #include <iostream>
4
2496
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
2644
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 specialization? I've tried a template on any type, like Outer<AnyT>::inner (see below) Also tried a template on the templated type: Outer<part<X> >::inner // (see below) I can't find a typo? What is the rule in operation here? Thanks, homsan
7
14909
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}'
6
1608
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
1404
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. #include <iostream> template <typename T> class TC
4
1887
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
8428
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, 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...
0
8341
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,...
0
8851
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, 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...
1
8539
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,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5650
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();...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
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 we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.