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

Template errors

I can't understand why this template code does not work:

//Print trace message. This one is OK.
template<typename T1, typename T2>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2)
{
if (t1 == true)
cout << t2 << "\n";
}

//Print trace message. This one is NOT OK!
template<typename T1, typename T2, typename T3>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2, T3 t3)
{
ostringstream temp;

temp << t2 << t3 << "\n";
eTmAuPtplRv_trace( t1, temp ); //Doesn't work! Why not? It should?

//This works.
//eTmAuPtplRv_trace( t1, temp.str() );
}

--The Directive
Jul 22 '05 #1
8 1343
On Sun, 11 Jan 2004 22:51:14 -0800, The Directive wrote:
I can't understand why this template code does not work:


Please explain what doesn't work. Are you having a problem compiling or is
the behaviour not what you expected? What exactly happens? What did you
expect to happen? We're not clairvoyant you know :-)

M4

Jul 22 '05 #2
On 11 Jan 2004 22:51:14 -0800, th***********@hotmail.com (The
Directive) wrote:
I can't understand why this template code does not work:

//Print trace message. This one is OK.
template<typename T1, typename T2>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2)
{
if (t1 == true)
cout << t2 << "\n";
}

//Print trace message. This one is NOT OK!
template<typename T1, typename T2, typename T3>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2, T3 t3)
{
ostringstream temp;

temp << t2 << t3 << "\n";
eTmAuPtplRv_trace( t1, temp ); //Doesn't work! Why not? It should?
It shouldn't. There is no operator<<(ostream, ostream)

//This works.
//eTmAuPtplRv_trace( t1, temp.str() );


Right, there is an operator<<(ostream, string).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
th***********@hotmail.com (The Directive) writes:
I can't understand why this template code does not work:

//Print trace message. This one is OK.
template<typename T1, typename T2>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2)
{
if (t1 == true)
cout << t2 << "\n";
}

//Print trace message. This one is NOT OK!
template<typename T1, typename T2, typename T3>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2, T3 t3)
{
ostringstream temp;

temp << t2 << t3 << "\n";
eTmAuPtplRv_trace( t1, temp ); //Doesn't work! Why not? It should?

//This works.
//eTmAuPtplRv_trace( t1, temp.str() );
}


Please post a *complete*, *compilable* example next time. Also, be more
specific than "doesn't work" - it might not compile, it might crash when
executed, it might produce not the expected output etc.

I guess you get an error message like

"std::ios_base::ios_base(const std::ios_base&)' is private"

This is because your functions eTmAuPtplRv_trace (BTW: get rid of this
f****ing pseudo-Hungarian notation - it makes parsing your code a PITA)
take their arguments by value, hence the copy-constructor of
ostringstream is invoked - since copying a stream doesn't make much sense,
it is private, therefore the error.
solution: change your functions to take their arguments by const reference
instead.

HTH & kind regards
frank

--
Frank Schmitt
quattro research gmbh phone: +49 89 700763-0
e-mail: schmitt NO at SPAM quattro-research !@! dot com
Jul 22 '05 #4
Martijn Lievaart <m@remove.this.part.rtij.nl> wrote in message news:<pa****************************@remove.this.p art.rtij.nl>...
On Sun, 11 Jan 2004 22:51:14 -0800, The Directive wrote:
I can't understand why this template code does not work:
Please explain what doesn't work. Are you having a problem compiling or is
the behaviour not what you expected? What exactly happens? What did you


It doesn't compile. It generates a lot of errors:

E:/DEV-CPP/include/c++/bits/ios_base.h: In copy constructor
`std::basic_ios<char, std::char_traits<char> >::basic_ios(const
std::basic_ios<char, std::char_traits<char> >&)':
Debug.h:102: instantiated from `static void Debug::trace(T1, T3, T2)
[with T1 = const char*, T2 = bool, T3 = std::basic_string<char,
std::char_traits<char>, std::allocator<char> >]'
Debug.h:116: instantiated from `static void Debug::trace(T1, T3, T4,
T2) [with T1 = const char*, T2 = bool, T3 = int, T4 =
std::basic_string<char, std::char_traits<char>, std::allocator<char>]' Debug.h:130: instantiated from `static void Debug::trace(T1, T3, T4,
T5, T2) [with T1 = const char*, T2 = bool, T3 = int, T4 = const char*,
T5 = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >]'
Debug.h:146: instantiated from `static void Debug::trace(T1, T3, T4,
T5, T6, T2) [with T1 = const char*, T2 = bool, T3 = int, T4 = const
char*, T5 = int, T6 = const char*]' eTfMgsMgrp_clsDrawingObj.cpp:22:
instantiated from here
E:/DEV-CPP/include/c++/bits/ios_base.h:421:
`std::ios_base::ios_base(const std::ios_base&)' is private
Debug.h:102: within this context E:/DEV-CPP/include/c++/streambuf: In
copy constructor `std::basic_stringbuf<char, std::char_traits<char>,
std::allocator<char> >::basic_stringbuf(const
std::basic_stringbuf<char, std::char_traits<char>,
std::allocator<char> >&)':
E:/DEV-CPP/include/c++/streambuf:486: `std::basic_streambuf<_CharT,
_Traits>::basic_streambuf(const std::basic_streambuf<_CharT,
_Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is
private
Debug.h:102: within this context Debug.h: In static member function
`static void Debug::trace(T1, T3, T2) [with T1 = const char*, T2 =
bool, T3 = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >]':
Debug.h:116: instantiated from `static void Debug::trace(T1, T3, T4,
T2) [with T1 = const char*, T2 = bool, T3 = int, T4 =
std::basic_string<char, std::char_traits<char>, std::allocator<char>]'

Debug.h:130: instantiated from `static void Debug::trace(T1, T3, T4,
T5, T2) [with T1 = const char*, T2 = bool, T3 = int, T4 = const char*,
T5 = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >]'
Debug.h:146: instantiated from `static void Debug::trace(T1, T3, T4,
T5, T6, T2) [with T1 = const char*, T2 = bool, T3 = int, T4 = const
char*, T5 = int, T6 = const char*]'

I expect it to compile.

--The Directive
Jul 22 '05 #5
tom_usenet <to********@hotmail.com> wrote in message news:<6m********************************@4ax.com>. ..
On 11 Jan 2004 22:51:14 -0800, th***********@hotmail.com (The
Directive) wrote:
I can't understand why this template code does not work:

//Print trace message. This one is OK.
template<typename T1, typename T2>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2)
{
if (t1 == true)
cout << t2 << "\n";
}

//Print trace message. This one is NOT OK!
template<typename T1, typename T2, typename T3>
inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2, T3 t3)
{
ostringstream temp;

temp << t2 << t3 << "\n";
eTmAuPtplRv_trace( t1, temp ); //Doesn't work! Why not? It should?


It shouldn't. There is no operator<<(ostream, ostream)


Why should this matter? The method contains template arguments that
should accept any type of parameters? Right? What does the "<<"
operator have to do with the method called?

--The Directive

//This works.
//eTmAuPtplRv_trace( t1, temp.str() );


Right, there is an operator<<(ostream, string).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Jul 22 '05 #6
On 12 Jan 2004 08:43:17 -0800, th***********@hotmail.com (The
Directive) wrote:
tom_usenet <to********@hotmail.com> wrote in message news:<6m********************************@4ax.com>. ..
On 11 Jan 2004 22:51:14 -0800, th***********@hotmail.com (The
Directive) wrote:
>I can't understand why this template code does not work:
>
>//Print trace message. This one is OK.
>template<typename T1, typename T2>
>inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2)
>{
> if (t1 == true)
> cout << t2 << "\n";
The above line attempts to cout t2.
>}
>
>//Print trace message. This one is NOT OK!
>template<typename T1, typename T2, typename T3>
>inline void clsDebug::eTmAuPtplRv_trace(T1 t1, T2 t2, T3 t3)
>{
> ostringstream temp;
>
> temp << t2 << t3 << "\n";
>
>
> eTmAuPtplRv_trace( t1, temp ); //Doesn't work! Why not? It should?


It shouldn't. There is no operator<<(ostream, ostream)


Why should this matter? The method contains template arguments that
should accept any type of parameters? Right? What does the "<<"
operator have to do with the method called?


The method called does "cout << t2"! So "cout << t2" has to be valid
for the passed object. It is for string, it isn't for ostringstream.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #7
On Mon, 12 Jan 2004 08:16:17 -0800, The Directive wrote:
Martijn Lievaart <m@remove.this.part.rtij.nl> wrote in message
news:<pa****************************@remove.this.p art.rtij.nl>...
On Sun, 11 Jan 2004 22:51:14 -0800, The Directive wrote:
> I can't understand why this template code does not work:
Please explain what doesn't work. Are you having a problem compiling or
is the behaviour not what you expected? What exactly happens? What did
you


It doesn't compile. It generates a lot of errors:

Frank was right:
E:/DEV-CPP/include/c++/bits/ios_base.h:421:
`std::ios_base::ios_base(const std::ios_base&)' is private


You pass an ostringstream by value as T2. streams cannot be passed by
value.

Even if you fix that, as Tom already noted, you would output a
ostringstream to cout, and that is not defined. Pass temp.str() instead
and it should work.

HTH,
M4
Jul 22 '05 #8
tom_usenet wrote in news:1l********************************@4ax.com:

Why should this matter? The method contains template arguments that
should accept any type of parameters? Right? What does the "<<"
operator have to do with the method called?


The method called does "cout << t2"! So "cout << t2" has to be valid
for the passed object. It is for string, it isn't for ostringstream.


Try it and see, I get std::operator << ( std::ostream &, void * ).

The won't compile problem is that you can't pass stream objects
by value as Frank Schmitt has pointed out up thread.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

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

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
1
by: terry.jeske | last post by:
Hello, In xalan 2.41 we were able to conditionally call a template. This appears broken in 2.6. I did glance at the call-template specfications but did not see anything that would point to this...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
4
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
3
by: Ruben Campos | last post by:
Greetings. Please, take a look to the next code, where I have a problem with specialization of class member functions. // ########## CPrinter.h ########## #ifndef PRINTER_H #define PRINTER_H ...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
1
by: Bill Rubin | last post by:
The following code gives VC++ 7.1 compiler errors C2785 and C2912 template<class U> struct Trait {typedef double result;} template<class T> typename Trait<T>::result foo() template<> typename...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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.