Änfängerproblem bei klassen-template
-
Hallo
ich möchte eine klasse matrix basteln
diese soll wie z.b. der vector mit: Matrix<double> testmatrix; erstellt werden.
dazu benötige ich templates
nun hab ich ein bissesl gegoogled und einige schöne Howtos gefunden
aber scheinbar bin ich zu blöde#ifndef MATRIX_H_ #define MATRIX_H_ //template<class MatTyp> //template<typename MatTyp> // das funzt net, warum ???? class Matrix { private: unsigned long m;// sicherstellen das m*n nicht überlaufen kann z.b.(sqrt < max(unsigned long)) unsigned long n; void* matrix; // über template public: Matrix(); Matrix(unsigned long m, unsigned long n); virtual ~Matrix(); Matrix operator +(Matrix op2); Matrix operator -(Matrix op2); Matrix operator *(Matrix op2); Matrix operator /(Matrix op2); }; #endif /*MATRIX_H_*/mein void* matrix soll eigentlich MatTyp* matrix sein
aber sobald ich den template-befehl einsetze bekomme ich folgende fehlermeldung
../src/Matrix.cpp:4: Fehler: »template<class MatTyp> class Matrix« ohne Template-Parameter verwendet ../src/Matrix.cpp:4: Fehler: ISO-C++ verbietet Deklaration von »Matrix« ohne Typ ../src/Matrix.cpp: In function »int Matrix()«: ../src/Matrix.cpp:4: Fehler: »int Matrix()« als andere Symbolart redeklariert ../src/Matrix.h:7: Fehler: vorherige Deklaration von »template<class MatTyp> class Matrix« ../src/Matrix.cpp: At global scope: ../src/Matrix.cpp:9: Fehler: expected constructor, destructor, or type conversion before »::« token ../src/Matrix.cpp:13: Fehler: »template<class MatTyp> class Matrix« ohne Template-Parameter verwendet ../src/Matrix.cpp:13: Fehler: ISO-C++ verbietet Deklaration von »Matrix« ohne Typ ../src/Matrix.cpp: In function »int Matrix(long unsigned int, long unsigned int)«: ../src/Matrix.cpp:13: Fehler: »int Matrix(long unsigned int, long unsigned int)« als andere Symbolart redeklariert ../src/Matrix.h:7: Fehler: vorherige Deklaration von »template<class MatTyp> class Matrix« ../src/Matrix.cpp:15: Fehler: »m« wurde in diesem Gültigkeitsbereich nicht definiert ../src/Matrix.cpp:16: Fehler: »n« wurde in diesem Gültigkeitsbereich nicht definiert ../src/Matrix.cpp: At global scope: ../src/Matrix.cpp:21: Fehler: ungültige Verwendung des Template-Namens »Matrix« ohne eine Argumentliste make: *** [src/Matrix.o] Fehler 1kann mir hier jemand weiterhelfen?
kennt jemand ein gutes howto zu templates mit klassen?
-
Zeig mal die zugehörige cpp-Datei. Btw müssen Template-Implementationen in der kompilierenden Einheit bekannt sein.
-
#include "Matrix.h" #include <exception> Matrix::Matrix() { } Matrix::~Matrix() { } Matrix::Matrix(unsigned long min, unsigned long nin) { m = min; n = nin; //matrix = new();//speicher für n*m elemente des typs ??? holen } Matrix Matrix::operator + (Matrix op2) { //Matrizen müssen gleiche größe haben if(m == op2.m && n == op2.n && m != 0 && n != 0) { Matrix tmp(m,n); for(unsigned long i = 0; i < m*n; i++) { //MatTyp tmp.matrix[i] = MatTyp matrix[i] + MatTyp op2.matrix[i];//wie geht das? } } else if(m != op2.m) { throw("111 Fehler bei der Matrizenaddition: Zeilenanzahl ist nicht gleich"); } else if(n != op2.n) { throw("112 Fehler bei der Matrizenaddition: Spaltenanzahl ist nicht gleich"); } else if(m == 0 || n == 0) { throw("113 Fehler bei der Matrizenaddition: Matrix ist leer"); } else { throw("110 Fehler bei der Matrizenaddition: Programmfehler in if-anweisung"); } return op2; }hab eigentlich hier noch nicht viel gemacht weil ich erstma das mit dem template gebacken kriegen will
-
template<typename MatTyp> Matrix::Matrix() { } uswIrgendwie so

edit: http://www.cplusplus.com/doc/tutorial/templates.html und http://www.is.pku.edu.cn/~qzy/cpp/vc-stl/templates.htm
-
hm hat auch net geholfen
aber ich werd wohl erstmal die tuts durchackern und mich morgen abend nochma melden
danke erstma soweit
-
habs doch noch gebacken gekriegt
[cpp]template<typename MatTyp>
Matrix**<MatTyp>**::Matrix()
{
}
usw[/cpp]hat gefehlt
vielen dank der zweite link hat mir sehr geholfen
auch wenn ich erst herausfinden musste, dass < == < ist
jetzt komm ich erstma wieder alleine zurecht