473,404 Members | 2,179 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,404 software developers and data experts.

Operator<<, in nested templated classes.

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>

using namespace std;

template<typename T>
class TemplatedOuter
{
public:
class NestedInner
{
public:
int a;

NestedInner() : a(0) {};
};
TemplatedOuter() : m_NestedInner() {};

NestedInner m_NestedInner;
};

/**
* This does not.
*/
template<typename T>
ostream operator<<(ostream &os, typename TemplatedOuter<T>::NestedInner
const &nestedInner)
{
}

/**
* This works...
*/
template<typename T>
ostream operator<<(ostream &os, TemplatedOuter<T> const
&templatedOuter)
{
}

int main(void)
{
TemplatedOuter<int> a;

// This works.
cout << a;

// This does not.
cout << a.m_NestedInner;
}
/**

Output:

main.cpp: In function `int main()':
main.cpp:39: error: no match for 'operator<<' in 'std::cout <<
a.TemplatedOuter<int>::m_NestedInner'

*/

As you can see, I'm trying to basically have a nested public class, and
print out the contents of it... But it can't seem to find it... Any
suggestions on what I'm doing wrong, would be appreciated!

Thanks!

Sep 9 '05 #1
1 1483
Eddie Parker wrote:
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. :)
You're running into "non-deducible context". It's not possible to deduce
the template argument from a template nested class. What's happening is
this: you give 'a.m_NestedInner' from which 'T' is supposed to be deduced.
The list of deducible contexts does *not* include template-name<T>::type.
Anyhow, here's a small test case that should compile on most compilers:

#include <iostream>

using namespace std;

template<typename T>
class TemplatedOuter
{
public:
class NestedInner
{
public:
int a;

NestedInner() : a(0) {};
Drop the traling semicolon from the line above.
};
TemplatedOuter() : m_NestedInner() {};
Drop the traling semicolon from the line above.

NestedInner m_NestedInner;
};

/**
* This does not.
*/
template<typename T>
ostream operator<<(ostream &os, typename TemplatedOuter<T>::NestedInner
const &nestedInner)
template<typename T>
ostream& ...
{
return os;
}

/**
* This works...
*/
template<typename T>
ostream operator<<(ostream &os, TemplatedOuter<T> const
template<typename T>
ostream& ...
&templatedOuter)
{
return os;
}

int main(void)
{
TemplatedOuter<int> a;

// This works.
cout << a;

// This does not.
cout << a.m_NestedInner;
}
/**

Output:

main.cpp: In function `int main()':
main.cpp:39: error: no match for 'operator<<' in 'std::cout <<
a.TemplatedOuter<int>::m_NestedInner'

*/

As you can see, I'm trying to basically have a nested public class, and
print out the contents of it... But it can't seem to find it... Any
suggestions on what I'm doing wrong, would be appreciated!


The only work-around I know is to specify the template argument explicitly
in a call:

::operator << <int> (cout, a.m_NestedInner);

Another work-around, of course, is to add a member function 'print' to the
'NestedInner' class and use it as

a.m_NestedInner.print(cout);

V
Sep 9 '05 #2

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): ...
4
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 }:
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...
0
by: Jerry Coffin | last post by:
In article <9476484e-a0cf-4aef-b22a- ea5f8920edbe@h1g2000prh.googlegroups.com>, smithco@gmail.com says... One possibility would be like this: #include <iostream> template <typename F>...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
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
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
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,...
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.