[TriLUG] OT: A C++ Templated Class Problem

Randy Barlow randy at electronsweatshop.com
Fri Apr 14 12:06:15 EDT 2006


Greetings all, I am trying to create a matrix class in C++ that will 
work with any type (i.e. templated).  The code I have seems to compile 
just fine, but I am getting the dreaded "unresolved external symbol" 
linker errors.  The weird thing is that if I hard code the type to 
double instead of making the class templated, the project seems to work 
just fine.  But I'll get the linker errors for the templated type.  
(This problem seems to exist under Windows and Linux both)  Here's my 
header file:

template <typename T>
class matrix {
private:
    T* myMatrix;
    int xsize, ysize, zsize;

public:
    matrix(void);
    ~matrix(void);

    //set size of the Matrix
    void setSize(int x, int y, int z);
    void setSize(int x, int y);
    void setSize(int x);

    //Set all of the values of the matrix to the initialValue
    void initialize(T initialValue);

    //how to access individual members
    T& operator() (int a, int b, int c);
    T& operator() (int a, int b);
    T& operator() (int a);
};

And for those who are interested, here's the .cpp file that goes along 
with it:

#include "matrix.h"

template <typename T>
matrix<T>::matrix() {
    xsize = 0;
    ysize = 0;
    zsize = 0;
}

template <typename T>
matrix<T>::~matrix() {
    delete [] myMatrix;
}

template <typename T>
void matrix<T>::setSize(int x, int y, int z) {
        xsize = x;
        ysize = y;
        zsize = z;
        myMatrix = new T[xsize*ysize*zsize];
}

template <typename T>
void matrix<T>::setSize(int x, int y) {
    xsize = x;
    ysize = y;
    zsize = 0;
    myMatrix = new T[xsize*ysize];
}

template <typename T>
void matrix<T>::setSize(int x) {
    xsize = x;
    ysize = 0;
    zsize = 0;
    myMatrix = new T[xsize];
}

//Set all of the values of the matrix to the initialValue
template <typename T>
void matrix<T>::initialize(T initialValue) {
    for(int i=0; i<xsize; i++) {
        for(int j=0; j<ysize; j++) {
            for(int k=0; k<zsize; k++) {
                (*this)(i,j,k) = initialValue;
            }
        }
    }
}

//how to access individual members
template <typename T>
T& matrix<T>::operator() (int a, int b, int c) {
    if(a>=0 && a<xsize && b>=0 && b<ysize && c>=0 && c<zsize)
        return myMatrix[a+b*xsize+c*xsize*ysize];
    else {
        std::cout << "Error: Exceeded matrix dimensions!" << std::endl;
        return myMatrix[0];
    }
}

template <typename T>
T& matrix<T>::operator() (int a) {
    if(a>=0 && a<xsize && ysize == 0 && zsize == 0)
        return myMatrix[a];
    std::cout << "Error: Exceeded matrix dimensions!" << std::endl;
    return myMatrix[0];
}

template <typename T>
T& matrix<T>::operator() (int a, int b) {
    if(a>=0 && a<xsize && b>=0 && b<ysize && zsize == 0)
        return myMatrix[a+b*xsize];
    std::cout << "Error: Exceeded matrix dimensions!" << std::endl;
    return myMatrix[0];
}

Keep in mind that this thing is still under development (I plan to 
implement exceptions instead of the cout statements, those are just for 
the debugging :))  Anyways, would appreciate any help you can offer!

Randy




More information about the TriLUG mailing list