模板类运算符重载问题 [英] template class operator overloading problem

查看:79
本文介绍了模板类运算符重载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试移植一些 Visual C++ (VS2005) 代码,以便它可以在 VS2005 和 C++ Builder XE 下编译.下面的代码在 VS2005 下编译得很好,但在 C++ Builder XE 下,我收到以下错误:

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 错误] time_stamp.h(49): E2327 运算符可能没有默认参数值

[BCC32 Error] time_stamp.h(49): E2327 Operators may not have default argument values

这是有问题的代码:(time_stamp.h)

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;

}

time_span 模板如下(time_span.h):

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 */
}

C++ Builder 编译器似乎不喜欢这一行:朋友 time_span 运算符-(const time_stamp &first, const time_stamp &second)

The C++ Builder compiler appears to not like the line: friend time_span operator-(const time_stamp &first, const time_stamp &second)

我是模板的新手,这种语法使我无法理解,或者至少是我无法理解的编译器错误.在我看来,尽管编译器这么说,但似乎没有默认参数值.我将错误消息解释为 const time_stamp &是一个默认值,当它在我看来就像一个传递的 time_stamp 类型的引用.

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.

---

如果我按如下方式重新构建上述调用:

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);
}

然后我收到此错误:

[BCC32 Error] time_stamp.hpp(56): E2082 '-(int (*)(const time_stamp &,const time_stamp &))' 必须是成员函数或具有类类型的参数

[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

推荐答案

[替换我之前的回答!]

[Replacing my previous answer!]

这可能与http://www.parashift.com有关/c++-faq/templates.html#faq-35.16 .

由于你要定义的函数实际上是一个模板函数(它取决于counter_type),你应该在friend声明之前声明它,然后声明一个函数模板实例化为 friend:

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);
}

这篇关于模板类运算符重载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆