Как создать обобщенный шаблонный класс?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Как создать обобщенный шаблонный класс?

Сообщение Anonymous »

Я немного занимался объектно-ориентированным программированием на C++, и у меня возник небольшой вопрос о том, что можно сделать при создании собственного класса.
Я попытался создать обобщенный класс шаблона из трех кортежей, который принимает любой тип (int, double, unsigned char и т. д.), но у меня возникли проблемы с одной из функций-членов:

Код: Выделить всё

template< typename structTypeA, typename structTypeB, typename structTypeC >
class striple {

private :

structTypeA first  ;

structTypeB second ;

structTypeC third  ;

public :

striple( ) : first { }, second { }, third{ }
{ }

striple( structTypeA firSetter ) : first { firSetter }, second { }, third{ }
{ }

striple( structTypeA firSetter, structTypeB secSetter ) : first { firSetter }, second { secSetter }, third{ }
{ }

striple( structTypeA firSetter, structTypeB secSetter, structTypeC thirSetter ) : first { firSetter }, second { secSetter }, third{ thirSetter }
{ }

const structTypeA getFir  ( ) ;

const structTypeB getSec  ( ) ;

const structTypeC getThir ( ) ;

template< typename memberOutType >
memberOutType get( const unsigned char INDEX ) ;

void resetFirst  ( structTypeA resetter ) ;

void resetSecond ( structTypeB resetter ) ;

void resetThird  ( structTypeC resetter ) ;

template< typename memberInType >
void reset( const unsigned char INDEX, const memberInType& resetter ) ;
};
Все работает, кроме функции-члена «reset» — это вызывает ошибку компилятора C2672.

'function': соответствующая перегруженная функция не найдена.

Я включил определения членов класса ниже, за которыми следует спецификация типа класса.

Код: Выделить всё

#include < striple.h >

template < typename structTypeA, typename structTypeB, typename structTypeC >
const structTypeA striple::getFir() { return first ; }

template< typename structTypeA, typename structTypeB, typename structTypeC >
const structTypeB striple::getSec() { return second ; }

template< typename structTypeA, typename structTypeB, typename structTypeC >
const structTypeC striple::getThir() { return third ; }

template< typename structTypeA, typename structTypeB, typename structTypeC >
template< typename memberOutType >
memberOutType striple::get( const unsigned char INDEX ) {

switch( INDEX ) {

case ITEM::FIRST :

return first ;

case ITEM::SECOND :

return second ;

case ITEM::THIRD :

return third ;
}
}

template< typename structTypeA, typename structTypeB, typename structTypeC >
void striple::resetFirst(structTypeA resetter) { first = resetter ; }

template< typename structTypeA, typename structTypeB, typename structTypeC >
void striple::resetSecond( structTypeB resetter ) { second = resetter ;}

template< typename structTypeA, typename structTypeB, typename structTypeC >
void striple::resetThird( structTypeC resetter ) { third = resetter ; }

template class striple< short unsigned     , short unsigned     , short unsigned      > ;
template class striple< unsigned           , unsigned           , unsigned            > ;
template class striple< long unsigned      , long unsigned      , long unsigned       > ;
template class striple< long long unsigned , long long unsigned , long long unsigned  > ;

template class striple< short int          , short int          , short int           > ;
template class striple< int                , int                , int                 > ;
template class striple< long int           , long int           , long int            > ;
template class striple< long long int      , long long int      , long long int       > ;

template class striple< float              , float              , float               > ;
template class striple< double             , double             , double              > ;
template class striple< long double        , long double        , long double         > ;

template class striple< short unsigned     , short unsigned     , short unsigned      > ;
template class striple< unsigned           , unsigned           , unsigned            > ;
template class striple< long unsigned      , long unsigned      , long unsigned       > ;
template class striple<  long long unsigned , long long unsigned , long long unsigned  > ;

template class striple< short int          , short int          , short int           > ;
template class striple< int                , int                , int                 > ;
template class striple< long int           , long int           , long int            > ;
template class striple< long long int      , long long int      , long long int       > ;

Код: Выделить всё

//The remaining specifications have been omitted for brevity
Вот main с простой попыткой сбросить один из элементов вектора и отобразить результат:

Код: Выделить всё

int main() {

striple s = {1, 2, 3} ;

s.reset(0, 5)

std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/79872452/how-to-make-a-generalized-template-class[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»