C++ 重载模板函数

C++ 重载模板函数,c++,templates,C++,Templates,在模板类的成员函数中,我需要一个用于int、float和string的比较。我的解决方案是让成员函数(DFA::entry)使用一个自制的comp函数:(templateint comp(tsu one,usu two)) 现在的问题是编译后,当程序启动时。当尝试在0x00000004位置写入时,我因访问冲突而引发异常 我只输入了我认为与问题相关的代码部分,因为没有comp函数,程序运行得非常好(但只适用于int) 这是让模板成员函数使用非成员模板函数的正确方法吗 #define EPSILON

在模板类的成员函数中,我需要一个用于int、float和string的比较。我的解决方案是让成员函数(DFA::entry)使用一个自制的comp函数:(templateint comp(tsu one,usu two))

现在的问题是编译后,当程序启动时。当尝试在0x00000004位置写入时,我因访问冲突而引发异常

我只输入了我认为与问题相关的代码部分,因为没有comp函数,程序运行得非常好(但只适用于int)

这是让模板成员函数使用非成员模板函数的正确方法吗

#define EPSILON 0.9

//Forward and non-member-Function Declarations
template< class dto_s, class dto_ea, int nQ, int nSigma, int nF > class DFA;

template <class T, class U>
int comp(T s_one, U s_two) {
    int ret_val = 0;
    if (s_two > s_two)
    {
        ret_val = ((s_two - s_one) < EPSILON);
    }
    if (s_one > s_two)
    {
        ret_val = ((s_one - s_two) < EPSILON);
    }
    return ret_val;
}

inline int comp(std::string s_one, std::string s_two) {
    //return abs(s_one.compare(s_two));
    return (s_one == s_two);
}



//Class Definition
template< class dto_s, class dto_ea, int nQ, int nSigma, int nF > class DFA
{
public:
    int entry(dto_ea[]);

    //Returns index of entry in Sigma
    int entry_to_number(dto_ea);
    //Returns index of state in Q
    int state_to_number(dto_s);

private:
    std::array<dto_s, nQ> Q;
    std::array<dto_ea, nSigma> Sigma;
    std::array<dto_s, nQ * nSigma> Delta;
    std::array<dto_s, nF> F;

    dto_s* current_state;
};


//              ENTRY FUNCTION
template<class dto_s, class dto_ea, int nQ, int nSigma, int nF>
inline int DFA<dto_s, dto_ea, nQ, nSigma, nF>::entry(dto_ea wort[])
{

    dto_s* temp_state = current_state;

    //these 2 functions work correctly and return a int
    row = state_to_number(*temp_state);
    column = entry_to_number(wort[i]);


    for (auto i = Q.begin(); i != Q.end(); i++)
    {
        if (comp<dto_s, dto_s>(Delta[row * nSigma, column], (*i))) {
            //do something
        }
    }
    for (auto i = F.begin(); i != F.end(); i++)
    {
        if (comp<dto_s, dto_s>((*temp_state), (*i))) {
            //do something
        }
    }


    return return_code;
}
#定义ε0.9
//转发和非成员函数声明
模板DFA类;
模板
国际比较(T s_1,U s_2){
int ret_val=0;
如果(s_-two>s_-two)
{
ret_val=((s_two-s_one)s_二)
{
ret_val=((s_one-s_two)class DFA
{
公众:
整数输入(dto_ea[]);
//返回以西格玛为单位的条目索引
int entry_至_编号(dto_ea);
//返回Q中的状态索引
int state_to_编号(dto_s);
私人:
std::阵列Q;
std::数组Sigma;
阵列增量;
std::数组F;
dto_s*当前_状态;
};
//输入函数
模板
内联int DFA::条目(dto_ea麦芽汁[])
{
dto_s*温度_状态=当前_状态;
//这两个函数正常工作并返回int
行=州至州编号(*临时州);
列=输入到编号(麦汁[i]);
对于(自动i=Q.begin();i!=Q.end();i++)
{
if(comp(增量[行*nSigma,列],(*i))){
//做点什么
}
}
对于(自动i=F.begin();i!=F.end();i++)
{
如果(comp((*temp_state),(*i))){
//做点什么
}
}
返回代码;
}

首先:这不是一个有效的浮点比较函数,因为它不是一个偏序(特别是,它不满足传递性:可以有a、b和c,这样a比较等价于b,b比较等价于c,但a不比较等价于c。)您不能将您的比较用于
std::map
std::sort
std::binary\u search
之类的内容

第二,你在比较
sutwo
sutwo
,我想你是想把它和
suone
进行比较。那也会引起问题


不过,这一比较同样存在致命缺陷。浮动的有效比较是
A
。无论你想用“ε”来解决什么问题,用“ε”来胡闹都是错误的解决方案。

使用调试器来捕获崩溃并在代码中找到它发生的位置。