Thread: New to class templates

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    New to class templates

    I'm new to class templates (templates in general, actually) and I'm trying to figure out why this won't compile.

    Code:
    // MATRIX.h
    #ifndef MATRIX_H
    #define MATRIX_H
    
    template <typename T>
    class Matrix {
    public:
            Matrix(int rows, int cols);
            Matrix(const Matrix &other);
            virtual ~Matrix();
    
            Matrix& operator=(const Matrix &rhs);
            T* operator[](int i);
            int getRows() const;
            int getCols() const;
    
    protected:
            void copy(const Matrix &other);
    
    private:
            Matrix();
            int m_rows;
            int m_cols;
            T *m_linArray;
    };
    
    #include "MATRIX.cpp"
    #endif /* MATRIX_H */
    Code:
    // MATRIX.cpp
    #include "MATRIX.h"
    
    template <typename T>
    Matrix<T>::Matrix()
    {}
    
    template <typename T>
    Matrix<T>::Matrix(int rows, int cols) {
            m_rows = rows;
            m_cols = cols;
            m_linArray = new T[m_rows * m_cols];
    }
    
    template <typename T>
    Matrix<T>::Matrix(const Matrix &other) {
            copy(other);
    }
    
    template <typename T>
    Matrix<T>::~Matrix() {
            delete[] m_linArray;
    }
    
    template <typename T>
    Matrix<T>& 
    Matrix<T>::operator=(const Matrix &other) {
            if( this != &other ) {
                    delete[] m_linArray;
                    copy(other);
            }
    
            return *this;
    }
    
    template <typename T>
    T* Matrix<T>::operator[](int i) {
            return m_linArray + (i*m_cols);
    }
    
    template <typename T>
    void 
    Matrix<T>::copy(const Matrix &other) {
            m_rows = other.m_rows;
            m_cols = other.m_cols;
    
            int size = m_rows * m_cols;
            m_linArray = new T[size];
            for( int i=0; i < size; i++ ) {
                    m_linArray[i] =
    		other.m_linArray[i];
            }
    }
    
    template <typename T>
    int Matrix<T>::getRows() const {
      return m_rows;
    }
    
    template <typename T>
    int Matrix<T>::getCols() const {
      return m_cols;
    }
    I'm getting the following errors:
    .\TEMP.cpp(4) : error C2143: syntax error : missing ';' before '<'
    1>.\TEMP.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>.\TEMP.cpp(4) : error C2988: unrecognizable template declaration/definition
    1>.\TEMP.cpp(4) : error C2059: syntax error : '<'
    1>.\TEMP.cpp(8) : error C2143: syntax error : missing ';' before '{'
    1>.\TEMP.cpp(8) : error C2447: '{' : missing function header (old-style formal list?)
    1>.\TEMP.cpp(15) : error C2143: syntax error : missing ';' before '<'
    1>.\TEMP.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>.\TEMP.cpp(15) : error C2086: 'int Matrix' : redefinition
    1> .\TEMP.cpp(4) : see declaration of 'Matrix'
    1>.\TEMP.cpp(15) : error C2988: unrecognizable template declaration/definition
    1>.\TEMP.cpp(15) : error C2059: syntax error : '<'
    1>.\TEMP.cpp(20) : error C2588: '::~Matrix' : illegal global destructor
    1>.\TEMP.cpp(20) : fatal error C1903: unable to recover from previous error(s); stopping compilation

    Is there something wrong with the syntax? Or perhaps I'm not including something I need to include? I've tried compiling this with both Visual C++ and g++ to no avail. Any help would be appreciated.
    Last edited by lisa1901; 09-30-2007 at 09:51 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    // MATRIX.cpp
    #include "TEMP.h"
    And this may come in handy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    One thing that jumps right out is that you #include a .cpp file inside a .h file. You should essentially never #include a .cpp file inside anything. In your case, you should #include the .h file in the .cpp file and not the other way around.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    That part is actually commented out in the version I had just tried compiling - even if I actually #include the correct file (#include "MATRIX.h"), it gives me those same 13 errors. I've actually read those FAQs before, too, and I can't figure out what's wrong with this.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    I #included the .cpp file in the .h file for 2 reasons: one, I've read that it's one of the ways to avoid a linker error (the other is putting all the implementation into the .h file itself, which I'm not allowed to do for this assignment), and two, the .h file was given to me that way and I can't modify it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    See if the following links help:

    http://www.velocityreviews.com/forum...es-in-vc-.html
    http://www.parashift.com/c++-faq-lit...html#faq-35.12

    Generally, with function and class templates, most people do put the whole implementation inside the header file, even though this would otherwise be bad practice, since most compilers don't implement the export keyword.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Rename MATRIX.cpp to MATRIX.hpp to avoid creating an unnecessary object file. That won't solve your problems though.

    One thing that's wrong in your code is that you use Matrix as a type. It's not. Matrix<T> is a type. That may fix some of your errors.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just put all the template code inside the header file. It has to be included in every module which uses the template class anyway.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by King Mir View Post
    One thing that's wrong in your code is that you use Matrix as a type. It's not. Matrix<T> is a type. That may fix some of your errors.
    Matrix is the same as Matrix<T> inside its member function definitions.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    Nevermind; I went to my TA today and realized it was a linker problem. Thanks guys for your help.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I've never heard of a syntax error being a linker problem.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM