在C ++

时间:2016-10-17 14:23:58

标签: c++ c++11 templates c++14

我对C ++有中等水平的知识,如果您发现在本博客中发布的问题非常简单或不符合标准,我想请原谅。但是,不知怎的,我无法解决它。 :)

您的帮助将不胜感激。这是我的代码: 类模板在.hpp文件中跟随:

template<typename T>
class FastRetinexFilter{
public:

    static T* getInstance();    
    T Adjust(cv::Mat source, cv::Mat &destination, bool adjustBrightness=true, bool adjustColors=true, int n=3, bool filterOnlyIllumination=false, bool guidedFilter=false, double adjustmentExponent=1.0/2.0, int kernelSize=25, int finalFilterSize=5, double upperBound=255.0);
    ......
    ......
    ......

private:
    .....
    ..... 
    static T *s_instance;
};

函数的定义如下:.cpp文件

#include "FastRetinexFilter.hpp"

using namespace cv;
template <class T> T* FastRetinexFilter<T>::s_instance = 0;

template <class T> T*
FastRetinexFilter<T>:: getInstance() {
    if (!s_instance)
        s_instance = new FastRetinexFilter();

    return s_instance;
}

template <class T> T FastRetinexFilter<T>::Adjust(cv::Mat source, cv::Mat &destination, bool adjustBrightness, bool adjustColors, int n, bool filterOnlyIllumination, bool guidedFilter, double adjustmentExponent, int kernelSize, int finalFilterSize, double upperBound){

    if (adjustBrightness==false && adjustColors==false){
        source.copyTo(destination);
        return;
    }

    cv::Mat gray;
    cvtColor(source, gray, COLOR_BGR2GRAY);
    ......
    ......
}    
......
......
...... 

现在,我想从另一个类调用adjust函数,其中我已经包含了头文件,并且FastRetinexFilter类的成员在那里可以正常显示。

我试着这样做:

FastRetinexFilter::getInstance()->Adjust(colorImgMat, result, brightnessAdjustment, colorCorrection, n, false, gif, a);

但它给了我错误。它说

  

“FastRetinexFilter”不是类,命名空间或枚举

请建议我如何使用getInstance()方法调用此函数模板。

当我不使用模板时,定义是这样的。我在其他课程中喜欢这样,这很好用:

FastRetinexFilter* FastRetinexFilter::instance = 0;

    FastRetinexFilter* FastRetinexFilter::getInstance() {
        if (!instance)
            instance = new FastRetinexFilter();

        return instance;
    }


In the header file the declaration is like this :

public:
    static FastRetinexFilter* getInstance();
        ......
        .....
private:
    static FastRetinexFilter* instance;
        ......
        ......

要从这个类中调用某个函数(例如connectedComponentLabeling),我会这样做:

FastRetinexFilter::getInstance()->connectedComponentLabeling(binImageCopy,numComponent);

但是我不明白,在模板的情况下如何做到这一点。 函数“getInstance()”将返回类FastRetinexFilter的对象指针。 所以根据你的答案,我应该这样做:

FastRetinexFilter<FastRetinexFilter*>::getInstance()->Adjust(...);

但这不起作用。 我该怎么办?请建议。我需要在此类中使用模板用于其他函数。因此,这里有必要使用类模板和函数模板。

1 个答案:

答案 0 :(得分:2)

您忘记了模板参数:

FastRetinexFilter<...>::getInstance()->Adjust(...);
                 ^^^^^
             Specify the type
相关问题