шаблони
3 отговора
194 прегледа
шаблони
интересува ме как стои въпроса с използване на шаблонна функция в клас който не е шаблонен. примерно такъв клас може да изглежда ето така.
class test
{
public:
template<class A> void display(A a);
};
template<class A> void test::display(A a)
{
return;
}
void main()
{
test _t;
_t.display(6);
//и тук почват сръдните:) този компилатор няма умилостивяване:)
}
каква точно ми е грешката? едно решение е да превърна целия клас в шаблонен обаче явно може и само функция да е шаблонна. понеже следното работи.
class test
{
public:
template<class A> void display(A a)
{
return;
}
};
void main()
{
test _t;
_t.display(6);
}
class test
{
public:
template<class A> void display(A a);
};
template<class A> void test::display(A a)
{
return;
}
void main()
{
test _t;
_t.display(6);
//и тук почват сръдните:) този компилатор няма умилостивяване:)
}
каква точно ми е грешката? едно решение е да превърна целия клас в шаблонен обаче явно може и само функция да е шаблонна. понеже следното работи.
class test
{
public:
template<class A> void display(A a)
{
return;
}
};
void main()
{
test _t;
_t.display(6);
}
Re: шаблони
Няма грешка - под VC 7.1 се компилира. По-скоро проблемът е в компилатора. Нали знаеш, че VC6 официално не поддържа member templates?
inline
пробвай да напишеш пред функцията inline. когато дефинираш тялото на функция направо в декларацията на класа (вариантът, който казваш, че работи) тя се смята за inline.
Re: шаблони
Ot MSDNa:
"SYMPTOMS
The following error message appears if you define a member template class or function outside the class:
error C2059: syntax error : ''template<''
error C2065: 'T' : undeclared identifier
CAUSE
The compiler does not support out-of-class definition of member template functions and classes. Please refer to the C++ Standard, section 14.5.2, for information on member templates.
"
"SYMPTOMS
The following error message appears if you define a member template class or function outside the class:
error C2059: syntax error : ''template<''
error C2065: 'T' : undeclared identifier
CAUSE
The compiler does not support out-of-class definition of member template functions and classes. Please refer to the C++ Standard, section 14.5.2, for information on member templates.
"