pajofego
2006-11-03, 14:39:09
Hallo miteinander,
ich bin's mal wieder mit meinem Problem rund um C++ :redface:
Ich habe mich mal an das Thema Template herangetraut. Ziel soll sein eine Template Klasse für Matrizen zu schreiben. Leider bin ich noch nicht sehr weit gekommen, da mir der Kompiler Fehlermeldungen bringt, mit der ich momentan nichts anfangen. Daher der Gang hier zu euch Experten ;)
Der Code:
Header File:
template <class T>class clMatrix
{
private:
void Delete();
protected:
int m_iNoRows;
int m_iNoCols;
T **m_pMatrix;
virtual void Init(const T& value);
virtual void CopyValues(const clMatrix<T>& srcMatrix);
public:
clMatrix();
clMatrix(int noRows, int noCols); // defaul constructor
clMatrix(int noRows, int noCols, const T& value); // fill all entries with value
clMatrix(const clMatrix<T>& srcMatrix); // copy constructor
void Resize(int noRows, int noCols); // Resize my Matrix
int GetCols();
int GetRows();
~clMatrix(); // destructor
};
cpp-Datei:
#include "clMatrix.h"
template<class T> clMatrix<T>::clMatrix()
{
Resize(0, 0);
}
template<class T> clMatrix<T>::clMatrix(int noRows = 0, int noCols = 0)
{
Resize(noRows, noCols);
}
template<class T> clMatrix<T>::clMatrix(int noRows, int noCols, const T& value)
{
Resize(noRows, noCols);
Init(value);
}
template<class T> clMatrix<T>::clMatrix(const clMatrix<T>& srcMatrix)
{
Resize(srcMatrix.GetRows(), srcMatrix.GetCols());
}
template<class T> void clMatrix<T>::Init(const T& value)
{
for(int i = 0; i<noRows; i++)
for(int j = 0; j<noCols; j++)
m_pMatrix[i][j] = value;
}
template<class T> void clMatrix<T>::Resize(int noRows, int noCols)
{
m_iNoRows = noRows;
m_iNoCols = noCols;
if (m_pMatrix != NULL)
Delete();
m_pMatrix = new T*[noRows];
for(int i = 0; i<noRows; i++)
m_pMatrix[i] = new T[noCols];
}
template<class T> void clMatrix<T>::CopyValues(const clMatrix<T>& srcMatrix)
{
if(noCols == srcMatrix.GetCols() || noRows == srcMatrix.GetRows())
{
for(int i = 0; i<noRows; i++)
for(int j = 0; j<noCols; j++)
m_pMatrix[i][j] = srcMatrix[i][j];
}
else
{
cerr << "Matrixlängen stimmen nicht überein!" << endl;
exit(1);
}
}
template<class T> void clMatrix<T>::Delete()
{
for(int i = 0; i<noRows; i++)
delete[] m_pMatrix[i];
delete[] m_pMatrix;
}
template<class T> int clMatrix<T>::GetRows()
{
return noRows;
}
template<class T> int clMatrix<T>::GetCols()
{
return noCols;
}
template<class T> clMatrix<T>::~clMatrix()
{
Delete();
}
Sodele, wenn ich folgenden Code in meiner Main schreibe:
int main(int argc, char *argv[])
{
//...
clMatrix<double> myMatrix(2,2);
//...
}
Meckert der Compiler mit folgender Meldung, mit der ich nichts anfangen kann:confused:
1>NOC_2D.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall clMatrix<double>::~clMatrix<double>(void)" (??1?$clMatrix@N@@QAE@XZ)" in Funktion "_main".
1>NOC_2D.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall clMatrix<double>::clMatrix<double>(int,int)" (??0?$clMatrix@N@@QAE@HH@Z)" in Funktion "_main".
Was mache ich falsch?
Bzw. bei folgendem Code:
cout << myMatrix.GetCols() << endl;
1>NOC_2D.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: int __thiscall clMatrix<double>::GetCols(void)" (?GetCols@?$clMatrix@N@@QAEHXZ)" in Funktion "_main".
gibt's diesen Fehler dazu :confused:
Auch hier natürlich vollkommene Ratlosigkeit vom Coder:confused:
Für eure Mühen und Hilfe schon mal vielen Dank,
Gruß
pajofego
ich bin's mal wieder mit meinem Problem rund um C++ :redface:
Ich habe mich mal an das Thema Template herangetraut. Ziel soll sein eine Template Klasse für Matrizen zu schreiben. Leider bin ich noch nicht sehr weit gekommen, da mir der Kompiler Fehlermeldungen bringt, mit der ich momentan nichts anfangen. Daher der Gang hier zu euch Experten ;)
Der Code:
Header File:
template <class T>class clMatrix
{
private:
void Delete();
protected:
int m_iNoRows;
int m_iNoCols;
T **m_pMatrix;
virtual void Init(const T& value);
virtual void CopyValues(const clMatrix<T>& srcMatrix);
public:
clMatrix();
clMatrix(int noRows, int noCols); // defaul constructor
clMatrix(int noRows, int noCols, const T& value); // fill all entries with value
clMatrix(const clMatrix<T>& srcMatrix); // copy constructor
void Resize(int noRows, int noCols); // Resize my Matrix
int GetCols();
int GetRows();
~clMatrix(); // destructor
};
cpp-Datei:
#include "clMatrix.h"
template<class T> clMatrix<T>::clMatrix()
{
Resize(0, 0);
}
template<class T> clMatrix<T>::clMatrix(int noRows = 0, int noCols = 0)
{
Resize(noRows, noCols);
}
template<class T> clMatrix<T>::clMatrix(int noRows, int noCols, const T& value)
{
Resize(noRows, noCols);
Init(value);
}
template<class T> clMatrix<T>::clMatrix(const clMatrix<T>& srcMatrix)
{
Resize(srcMatrix.GetRows(), srcMatrix.GetCols());
}
template<class T> void clMatrix<T>::Init(const T& value)
{
for(int i = 0; i<noRows; i++)
for(int j = 0; j<noCols; j++)
m_pMatrix[i][j] = value;
}
template<class T> void clMatrix<T>::Resize(int noRows, int noCols)
{
m_iNoRows = noRows;
m_iNoCols = noCols;
if (m_pMatrix != NULL)
Delete();
m_pMatrix = new T*[noRows];
for(int i = 0; i<noRows; i++)
m_pMatrix[i] = new T[noCols];
}
template<class T> void clMatrix<T>::CopyValues(const clMatrix<T>& srcMatrix)
{
if(noCols == srcMatrix.GetCols() || noRows == srcMatrix.GetRows())
{
for(int i = 0; i<noRows; i++)
for(int j = 0; j<noCols; j++)
m_pMatrix[i][j] = srcMatrix[i][j];
}
else
{
cerr << "Matrixlängen stimmen nicht überein!" << endl;
exit(1);
}
}
template<class T> void clMatrix<T>::Delete()
{
for(int i = 0; i<noRows; i++)
delete[] m_pMatrix[i];
delete[] m_pMatrix;
}
template<class T> int clMatrix<T>::GetRows()
{
return noRows;
}
template<class T> int clMatrix<T>::GetCols()
{
return noCols;
}
template<class T> clMatrix<T>::~clMatrix()
{
Delete();
}
Sodele, wenn ich folgenden Code in meiner Main schreibe:
int main(int argc, char *argv[])
{
//...
clMatrix<double> myMatrix(2,2);
//...
}
Meckert der Compiler mit folgender Meldung, mit der ich nichts anfangen kann:confused:
1>NOC_2D.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall clMatrix<double>::~clMatrix<double>(void)" (??1?$clMatrix@N@@QAE@XZ)" in Funktion "_main".
1>NOC_2D.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall clMatrix<double>::clMatrix<double>(int,int)" (??0?$clMatrix@N@@QAE@HH@Z)" in Funktion "_main".
Was mache ich falsch?
Bzw. bei folgendem Code:
cout << myMatrix.GetCols() << endl;
1>NOC_2D.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: int __thiscall clMatrix<double>::GetCols(void)" (?GetCols@?$clMatrix@N@@QAEHXZ)" in Funktion "_main".
gibt's diesen Fehler dazu :confused:
Auch hier natürlich vollkommene Ratlosigkeit vom Coder:confused:
Für eure Mühen und Hilfe schon mal vielen Dank,
Gruß
pajofego