C 模板 - 主要表达式

C++ Template - primary expression needed

本文关键字:表达式 模板      更新时间:2023-10-16

错误在使用gcc/g 编译器在cygwin环境下编译以下程序 -/src/template_1.cpp:66:2:错误:"模板"之前预期的主要表达 模板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;
}

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

TemplateCall<int> obj;

更新,响应OP的评论

要明确实例化类模板,请使用

template class TemplateCall<int>;

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