c++ 主函数中的模板错误? [英] c++ Template error in main function?

查看:24
本文介绍了c++ 主函数中的模板错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 VS c++ 6.0.我已经读到 6.0 的模板有一些问题??

I am using VS c++ 6.0. I have read that 6.0 has some problems with templates??

好的...如果我将声明保留为:

OK... if I leave the declaration as:

template <class T> T jMin( T a, T b ){
    return ( a < b );
}

该函数有效,但执行以下操作时出现错误:

The function works, but doing as the following I get the error:

error C2039: 'jMin' : is not a member of 'CVid3Dlg'

为什么会有区别?...这可能与之前的帖子有关...

Why is there a difference?... and this may relate to the previous post...

如果我把定义放在 HEADER 中,我得到:

If I put the definition in the HEADER as follows, I get:

error C2893: Failed to specialize function template 'T __thiscall CVid3Dlg::jMin(T,T)'
        With the following template arguments:
        'double'

//CVid3Dlg.h

// CVid3Dlg.h

class CVid3Dlg : public CDialog
{
public:
    CVid3Dlg(CWnd* pParent = NULL); // standard constructor
    template <typename T>  T jMin( T a, T b );

protected:
    HICON m_hIcon;
    bool PreViewFlag;

    BITMAP bm; //bitmap struct
    CBitmap m_bmp; //bitmap object
    CRect m_rectFrame; //capture frame inside main window
    bool firstTime;



    // Generated message map functions
    //{{AFX_MSG(CVid3Dlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void GetVideo();
    afx_msg void OnClose();
    afx_msg void testing();
    afx_msg void Imaging();
    afx_msg void exTemplate();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//CVid3Dlg.cpp

//CVid3Dlg.cpp

template <class T> T CVid3Dlg::jMin( T a, T b ){// <-- FAILS
    return ( a < b );
}

void CVid3Dlg::exTemplate()
{
    Image *im = new Image();
    int s=0;

    s = jMin((double)3, (double)4);

    CString s1;

    s1.Format("%d", s);
    MessageBox(s1);
}

推荐答案

好的,您已经将模板设置为 CVid3Dlg 的成员函数.现在在 CVid3Dlg::exTemplate() 中,您可以按如下方式使用它:

OK, you've made the template a member function of CVid3Dlg. Now in CVid3Dlg::exTemplate() you can use it as follows:

 CVid3Dlg::exTemplate() 
 {
       double min = jMin<double>(3.0, 3.1);

 }

这篇关于c++ 主函数中的模板错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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