Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

template Klasse

Empfohlene Antworten

Veröffentlicht

Hi zusammen,

ich habe folgendes Problem: Ich habe folgende Template-Klasse implementiert:

template<class T, int i>class Array

{

T m_set;

int m_size;

public:

Array():m_size(i){};

void SetAt(T value, int index){ m_set[index] = value; }

T GetAt(int index){ return m_set[index]; }

int GetSize(){ return ( sizeof(m_set) / sizeof(T) ); }

void Sort(){ qsort( (void *)m_set, GetSize(), sizeof(T), compare ); }

};

das ganze wird dann folgendermaßen instanziiert, gefüllt, sortiert und ausgegeben:

int compare( const void *arg1, const void *arg2 );

int main()

{

Array< double, 5> DoubleArray;

Array< int, 5> IntArray;

int nSize = DoubleArray.GetSize();

for( int k = 0; k < nSize; k++ )

{

DoubleArray.SetAt( (double)rand(), k );

IntArray.SetAt( rand(), k );

}

DoubleArray.Sort();

IntArray.Sort();

for( int m = 0; m < nSize; m++ )

{

printf( "Wert an Index %d : %f\n", m, DoubleArray.GetAt(m) );

printf( "Wert an Index %d : %d\n", m, IntArray.GetAt(m) );

}

return 0;

}

nun zu meinem Problem. Ich habe bis jetzt folgend zwei Versionen der Funktion 'compare':

int compare( const void *arg1, const void *arg2 )

{

int result = 0;

if( * ( int* ) arg1 < * ( int* ) arg2 )

result = -1;

if( * ( int* ) arg1 > * ( int* ) arg2 )

result = 1;

return result;

}

int compare( const void *arg1, const void *arg2 )

{

int result = 0;

if( * ( double* ) arg1 < * ( double* ) arg2 )

result = -1;

if( * ( double* ) arg1 > * ( double* ) arg2 )

result = 1;

return result;

}

(wobei nur eine implementiert ist, die andere ist auskommentiert, klar warum). So nun würde ich gerne eine TemplateFunktion schreiben, welche die übergebenen Parameter entsprechend umwandelt. Im Klartext eine Funktion 'compare', welche ja nach Bedarf die Argumente 'arg1' und 'arg2' in entweder eien int oder einen double castet. Wäre nett wenn mir jemand einen kleinen Stoss in die richtige Richtung geben könnte.

Danke im vorraus

wenn ich dein problem richtig verstehe sollte es ein einfaches funktionstemplate tun.

 
template<class T> int compare (const T a, const T
{
return a < b ? -1 : 0;
}

main()
{
int i = compare (12, 20);
double d = compare (1.34, 4.56);
}
[/PHP]

siehe: http://www.luzifers-hp.de/cpp/funktionen.shtml#funktionstemplates

Genau.

Etwas ähnliches hatte ich auch schon. Nur generiert der Compiler dabei folgende Fehlermeldung:

f:templateclass\templateclass.h(12) : error C2664: 'qsort' : Konvertierung des Parameters 4 von 'int (const ,const )' in 'int (__cdecl *)(const void *,const void *)' nicht moeglich

Keine Funktion mit diesem Namen im Gueltigkeitsbereich stimmt mit dem Zieltyp ueberein

f:\templateclass\templateclass.h(12) : Bei der Kompilierung der Member-Funktion 'void __thiscall Array<char *,6>::Sort(void)' der Klassenvorlage

Und ich werd daraus einfach nicht schlau.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.