Reputation: 1697
I'm trying to port some Visual C++ (VS2005) code so that it will compile under both VS2005 and C++ Builder XE. The code that follows compiles fine under VS2005 but under C++ Builder XE, I get the following error:
[BCC32 Error] time_stamp.h(49): E2327 Operators may not have default argument values
Here is the code in question: (time_stamp.h)
template<typename counter_type>
class time_stamp
{
typedef time_span<counter_type> time_span_type;
typedef typename counter_type::value_type value_type;
public:
/* some code removed for sake of this post, next line is the one causing the error */
friend time_span<counter_type> operator-(const time_stamp<counter_type> &first, const time_stamp<counter_type> &second)
{
return time_span<counter_type>(first.m_stamp - second.m_stamp);
}
private:
value_type m_stamp;
}
The time_span template is as follows (time_span.h):
template<typename counter_type>
class time_span
{
public:
// TYPES
typedef counter_type counter_type;
typedef typename counter_type::value_type value_type;
/* rest removed for sake of this post */
}
The C++ Builder compiler appears to not like the line: friend time_span operator-(const time_stamp &first, const time_stamp &second)
I'm new to templates and this syntax escapes me or at least the compiler error I can't make sense of. It appears, to me, that there are no default argument values despite the compiler saying so. I'm interpreting the error message as saying const time_stamp & is a default value when it looks to me like a passed reference of type time_stamp.
Thanks for reading & replying. Help understanding as well as fixing is most appreciated.
--- EDIT:
If I re-structure the call above as follows:
friend time_span<counter_type> operator-( (const time_stamp<counter_type>& first, const time_stamp<counter_type>& second) );
and outside the class definition, I describe the function:
template<typename counter_type>
time_span<counter_type> operator-( (const time_stamp<counter_type>& first, const time_stamp<counter_type>& second) )
{
return time_span<counter_type>(first.m_stamp-second.m_stamp);
}
I then get this error:
[BCC32 Error] time_stamp.hpp(56): E2082 '-(int (*)(const time_stamp &,const time_stamp &))' must be a member function or have a parameter of class type
Upvotes: 2
Views: 1632
Reputation: 72271
[Replacing my previous answer!]
This is probably related to http://www.parashift.com/c++-faq/templates.html#faq-35.16 .
Since the function you want to define is in fact a template function (it depends on counter_type
), you should declare it before the friend
declaration, and then declare a function template instantiation to be the friend
:
// time_stamp.h
#include "time_span.h"
template<typename counter_type> class time_stamp;
template<typename counter_type> time_span<counter_type> operator-(
const time_stamp<counter_type>&, const time_stamp<counter_type>& );
template<typename counter_type>
class time_stamp
{
//...
friend time_span<counter_type> operator- <> (
const time_stamp<counter_type>&, const time_stamp<counter_type>&);
};
template<typename counter_type>
inline time_span<counter_type> operator-(
const time_stamp<counter_type>& first, const time_stamp<counter_type>& second)
{
return time_span<counter_type>(first.m_stamp - second.m_stamp);
}
Upvotes: 1
Reputation: 51465
try as:
template<class counter_type>
class .... {
friend time_span<counter_type> operator-(const time_stamp<counter_type> &first, const time_stamp<counter_type> &second);
};
template<class counter_type>
time_span<counter_type> operator-(const time_stamp<counter_type> &first,
const time_stamp<counter_type> &second)
{
return time_span<counter_type>(first.m_stamp - second.m_stamp);
}
Upvotes: 0