C ++模板 - 需要主表达式

时间:2017-04-17 05:55:11

标签: c++ c++11 templates

在cygwin环境下使用GCC / G ++编译器编译以下程序时出错 - /src/Template_1.cpp:66:2:错误:在'模板'之前预期的primary-expression   template TemplateCall :: TemplateCall;   ^

我的源代码如下: -

#include<iostream>
#include<cstdarg>


using namespace std;

template<class T>
class TemplateCall
{
        private:
                T dataValue;
        public:
                TemplateCall(T somethingValue);
                void showTTParam();
};


template<class T>
TemplateCall<T>::TemplateCall(T somethingValue)
{
        cout << endl << " Calling TemplateCall - constructor " << endl;
        dataValue = somethingValue;
}

template<class T>
void TemplateCall<T>::showTTParam()
{
        cout << endl << " TemplateTemplateParam - showTTParam " << endl;
        cout << endl << " dataValue - showTTParam " << dataValue << endl;
}



int main()
{

        template TemplateCall<int>;

        return 0;
}

1 个答案:

答案 0 :(得分:5)

要创建TemplateCall<int>的实例,您需要使用:

TemplateCall<int> obj;

更新,以回应OP的评论

要显式实例化类模板,请使用

template class TemplateCall<int>;

但你需要在所有功能之外使用它。

相关问题