Archiv verlassen und diese Seite im Standarddesign anzeigen : template für struct verwenden
hallo ich habe folgendes template welches ich zb für integer werte nutzen kann..
aber wie nutze ich es für structs?
#ifndef _TEMPLATE_H
#define _TEMPLATE_H
#include "exceptions.h"
#endif _TEMPLATE_H
template <typename T, int s> class Array
{
private:
/* pointer to array values */
T* aptr;
int size;
public:
/* constructor with array size */
Array()
{
this->aptr = (T*) calloc(s, sizeof(T));
size = s;
}
void changeSize (int s)
{
T* pTemp;
if ( (pTemp = (T*) calloc(s, sizeof(T)) ) == NULL){
throw ("fatal error with calloc!");
}
*pTemp = *this->aptr; // copy values
free (this->aptr); // free old pointer
this->aptr = pTemp; // copy "address area"
size = s;
}
int showArraySize ()
{
return size;
}
/* write in array at specified index */
Array operator () (T val, int index)
{
if ( (index >= 0) && (index < size))
this->aptr [index] = val;
else{
myException e;
e.setname("index out of bounds! can not write in array!");
throw e;
}
}
/* returns the value of the specified index */
T showArrayValue (int index)
{
/* check for valid size of array */
if ( (index >= 0) && (index < size))
return (aptr[index]);
else{
myException e;
e.setname ("out of bounds! can not return the value of the element!");
throw e;
}
}
};
in main kann leg ich mir dann zb für int so ein array an
Array <int, 10> intArray;
und beschreibe es dann:
intArray (2,9);
Würdest du das ganze bitte in [code]-Tags nochmal posten. Es ist so absolut unleserlich.
sorry
#ifndef _TEMPLATE_H
#define _TEMPLATE_H
#include "exceptions.h"
#endif _TEMPLATE_H
template <typename T, int s> class Array
{
friend int main (int argc, char argv[]);
private:
/* pointer to array values */
T* aptr;
int size;
public:
/* constructor with array size */
Array()
{
this->aptr = (T*) calloc(s, sizeof(T));
size = s;
}
void changeSize (int s)
{
T* pTemp;
if ( (pTemp = (T*) calloc(s, sizeof(T)) ) == NULL){
throw ("fatal error with calloc!");
}
*pTemp = *this->aptr; // copy values
free (this->aptr); // free old pointer (smaller array)
this->aptr = pTemp; // copy "address area"
size = s;
}
int showArraySize ()
{
return size;
}
/* write in array at specified index */
Array operator () (T val, int index)
{
if ( (index >= 0) && (index < size))
this->aptr [index] = val;
else{
myException e;
e.setname("index out of bounds! can not write in array!");
throw e;
}
}
/* returns the value of the specified index */
T showArrayValue (int index)
{
/* check for valid size of array */
if ( (index >= 0) && (index < size))
return (aptr[index]);
else{
myException e;
e.setname ("out of bounds! can not return the value of the element!");
throw e;
}
}
};
in main kann leg ich mir dann zb für int so ein array an
Array <int, 10> intArray;
und beschreibe es dann:
intArray (2,9);
aber wie kann ich das template für eine struct verwenden?
Neomi
2010-07-24, 13:49:25
aber wie kann ich das template für eine struct verwenden?
Genau so wie jetzt, nur mit einer Struktur statt int. Warum sollte es denn nicht funktionieren?
Abgesehen davon, daß das Template noch sehr unschön ist (kein Support für Klassen mit Konstruktoren und Destruktoren, umständliche Handhabung und noch ein paar weitere Kleinigkeiten), hast du noch eine Bug drin. Beim Ändern der Größe willst du die enthaltenen Werte kopieren, kopierst tatsächlich aber nur den ersten.
PS: Warum braucht main Zugriff auf die Interna? Ist doch alles über das öffentliche Interface erreichbar.
na wenn ichs genauso mache wie für ein int wills eben nich...
angenommen ich habe die struct:
struct arrayTest
{
double x;
};
und so leg ich das an:
Array <struct arrayTest, 8> structArray
wie beschreibe ich dann das array?
und danke für den hinweis ich dachte so werden alle werte kopiert... mist ^^
PS: Warum braucht main Zugriff auf die Interna? Ist doch alles über das öffentliche Interface erreichbar.
da hab ich nur was ausprobiert, sollte nich mehr im code sein...
Neomi
2010-07-24, 14:41:12
wie beschreibe ich dann das array?
Indem du statt dem Integer eine Instanz deiner Struktur übergibst. Die kannst du z.B. vorher auf dem Stack anlegen und mit Werten befüllen.
Warum benutzt du eigentlich nicht einfach std::vector?
ja richtig und genau da liegt das problem.
ich hab kein schimmer wie es der compiler annehmen würde.
Warum benutzt du eigentlich nicht einfach std::vector?
a) das soll so oder so ähnlich sein.
b) weil wenn ich alles könnte hier nicht so blöd fragen würde.. ;)
aber danke soweit
structArray(arrayTest(), 1);
Ich finde es äußerst unschön, dass du operator() für insert überlädst. Das sieht total komisch aus.
welchen operator könnte ich den dafür noch überladen, ich wil ja zwei werte übergeben?
wie muss das dann aussehen?
Einfach ne Memberfunktion "replace" o.ä. So das es klar ist.
ist vorgegeben, dass wir das array mit ner eigenen operatorfunktion beschreiben...
vielleicht habe ich da aber auch etwas fehlinterpretiert.. ;(
Dann vermute ich mal er will, dass du operator[] überlädst.
T &operator[] (int index)
{
if ( (index >= 0) && (index < size))
return aptr [index];
else {
myException e;
e.setname("index out of bounds! can not write in array!");
throw e;
}
}
Dann kannst du das ganze so verwenden: structArray[0] = arrayTest();
Lesen geht dann damit natürlich auch.
ok danke nochmals...
ich mag menschen die einfach antworten um zu helfen ;)
del_4901
2010-07-24, 20:28:44
structArray(arrayTest(), 1);
Ich finde es äußerst unschön, dass du operator() für insert überlädst. Das sieht total komisch aus.
Also er währe damit nicht allein. Die UE3 macht das auch so. :-/
vBulletin®, Copyright ©2000-2024, Jelsoft Enterprises Ltd.