专业化功能从其他功能调用

时间:2016-09-19 00:20:57

标签: c++ templates specialization

我试图想要从另一个调用模板特化功能。在下面的短段中,我试图从RED调用BLACK特定函数。我得到以下编译器错误

无法调用成员函数bool noClass<color>::_open() [with Colors color = (Colors)2]’ without object

这非常有意义。怎样(或者我可以)从RED工作中调用BLACK?代码段位于noClass C ++标题下面。

#ifndef COLORS_H
#define COLORS_H

enum class Colors { RED, GREEN, BLACK };

#endif /* COLORS_H */

#ifndef NOCLASS_H
#define NOCLASS_H


    template <Colors color> 
    class noClass {
        public: bool Open ( );
        protected: bool _open ( );
    };

    template <Colors color> bool noClass<color>::Open ( ) { return noClass<color>::_open ( ); }

    template <Colors color> bool noClass<color>::_open ( ) { return true; }

    template <> inline bool noClass<Colors::BLACK>::_open ( ) { return true; }

    template <> inline bool noClass<Colors::RED>::_open ( ) { return noClass<Colors::BLACK>::_open( ); }

#endif /* NOCLASS_H */

1 个答案:

答案 0 :(得分:0)

您不能,因为您没有所需类型的实例。尝试在辅助函数中编写公共代码,并让两个特化调用它。