PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : template für struct verwenden


ast
2010-07-24, 13:22:01
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);

Coda
2010-07-24, 13:31:36
Würdest du das ganze bitte in [code]-Tags nochmal posten. Es ist so absolut unleserlich.

Gast
2010-07-24, 13:38:36
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.

Gast
2010-07-24, 13:56:34
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 ^^

Gast
2010-07-24, 13:58:06
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?

Gast
2010-07-24, 14:56:20
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

Coda
2010-07-24, 15:56:32
structArray(arrayTest(), 1);

Ich finde es äußerst unschön, dass du operator() für insert überlädst. Das sieht total komisch aus.

Gast
2010-07-24, 16:11:18
danke dir

Gast
2010-07-24, 16:31:07
welchen operator könnte ich den dafür noch überladen, ich wil ja zwei werte übergeben?

wie muss das dann aussehen?

Coda
2010-07-24, 16:35:51
Einfach ne Memberfunktion "replace" o.ä. So das es klar ist.

Gast
2010-07-24, 16:39:21
ist vorgegeben, dass wir das array mit ner eigenen operatorfunktion beschreiben...

vielleicht habe ich da aber auch etwas fehlinterpretiert.. ;(

Coda
2010-07-24, 17:00:32
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.

Gast
2010-07-24, 17:28:03
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. :-/