Template errors

T

The Directive

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
 
M

Martijn Lievaart

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
 
T

tom_usenet

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
 
F

Frank Schmitt

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
 
T

The Directive

Martijn Lievaart said:
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 =
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 =
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
 
T

The Directive

tom_usenet said:
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
 
T

tom_usenet

M

Martijn Lievaart

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
 
R

Rob Williscroft

tom_usenet wrote in
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::eek:perator << ( std::eek:stream &, 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top