Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模板类运算符重载问题 我试图移植一些Visual C++(VS2005)代码,以便在VS2005和C++ Builder XE下编译。下面的代码在VS2005下编译成精细,但是在C++ Builder XE下,我得到以下错误:_C++_Templates_Operator Overloading - Fatal编程技术网

模板类运算符重载问题 我试图移植一些Visual C++(VS2005)代码,以便在VS2005和C++ Builder XE下编译。下面的代码在VS2005下编译成精细,但是在C++ Builder XE下,我得到以下错误:

模板类运算符重载问题 我试图移植一些Visual C++(VS2005)代码,以便在VS2005和C++ Builder XE下编译。下面的代码在VS2005下编译成精细,但是在C++ Builder XE下,我得到以下错误:,c++,templates,operator-overloading,C++,Templates,Operator Overloading,[BCC32错误]时间戳.h(49):E2327运算符可能没有默认参数值 下面是有问题的代码:(time_stamp.h) 模板 上课时间戳 { typedef time_span time_span_type; typedef typename计数器\类型::值\类型值\类型; 公众: /*为了这篇文章删除了一些代码,下一行就是导致错误的代码*/ 友元时间跨度运算符-(常数时间戳&第一,常数时间戳&第二) { 返回时间间隔(first.m_stamp-second.m_stamp); } 私人

[BCC32错误]时间戳.h(49):E2327运算符可能没有默认参数值

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

模板
上课时间戳
{
typedef time_span time_span_type;
typedef typename计数器\类型::值\类型值\类型;
公众:
/*为了这篇文章删除了一些代码,下一行就是导致错误的代码*/
友元时间跨度运算符-(常数时间戳&第一,常数时间戳&第二)
{
返回时间间隔(first.m_stamp-second.m_stamp);
}
私人:
价值型m_印章;
}
时间跨度模板如下(time_span.h):

模板
上课时间
{
公众:
//类型
类型定义计数器\类型计数器\类型;
typedef typename计数器\类型::值\类型值\类型;
/*为了这篇文章,其余的都被删除了*/
}

C++的Builder编译器看起来不喜欢这行: 友元时间跨度运算符-(常数时间戳&第一,常数时间戳&第二)

我对模板还不熟悉,这种语法让我不知所措,或者至少是我无法理解的编译器错误。在我看来,尽管编译器这么说,但似乎没有默认参数值。我将错误消息解释为:const time_stamp&在我看来是time_stamp类型的传递引用时,它是一个默认值

感谢您的阅读和回复。非常感谢帮助理解和修复

---编辑:

如果我将上述通话重新组织如下:

friend time_span<counter_type> operator-( (const time_stamp<counter_type>& first, const time_stamp<counter_type>& second) );
友元时间跨度运算符-((常数时间戳&第一,常数时间戳&第二));
在类定义之外,我描述了函数:

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);
}
模板
时间跨度运算符-(常数时间戳和第一个,常数时间戳和第二个))
{
返回时间间隔(first.m_stamp-second.m_stamp);
}
然后我得到这个错误:


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

[替换我以前的答案!]

这很可能是因为

由于要定义的函数实际上是一个模板函数(它取决于
计数器类型
),因此应该在
友元
声明之前声明它,然后将函数模板实例化声明为
友元

// 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);
}
//时间戳.h
#包括“时间跨度h”
模板类时间戳;
模板时间跨度运算符-(
常数时间戳&,常数时间戳&);
模板
上课时间戳
{
//...
朋友时间跨度运算符-(
常数时间戳&,常数时间戳&);
};
模板
内联时间跨度运算符-(
常数时间戳和第一个,常数时间戳和第二个)
{
返回时间间隔(first.m_stamp-second.m_stamp);
}
尝试:

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);
}
模板
班级。。。。{
友元时间跨度运算符-(常数时间戳&第一,常数时间戳&第二);
};
模板
时间跨度运算符-(常数时间戳&第一,
施工时间戳(秒)
{
返回时间间隔(first.m_stamp-second.m_stamp);
}

我试过了,但没有解决我的问题;我在朋友热线上收到了完全相同的错误消息。除了最后一个我认为是的选项外,我已经尝试了所有这些选项。如果我理解正确的话,“注入的类名时间戳标识符”是指用时间戳或时间戳替换时间戳?不,我的意思是用
时间戳替换
时间戳。不过,我怀疑这会有什么不同。请参见替换答案。在编辑中:
)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);
}
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);
}