Thread: Cannot Instantiate Object from Templated Class

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Cannot Instantiate Object from Templated Class

    I have a Matrix Class defined as follows:
    Code:
    #include "matrix.h"
    
    template <class Object>
    Matrix<Object>::Matrix(int row, int col)
    {
        rows = row;
        cols = col;
        array.resize(row);
        for(int r = 0; r < row; r++){
            array[r].resize(col);
        }
    }
    
    
    template <class Object>
    void Matrix<Object>::setValue(Object value, int r, int c)
    {
        array[r,c] = value;
    }
    
    
    template <class Object>
    Object Matrix<Object>::GetValue(int r, int c) const
    {
        return array[r][c];
    }
    
    
    template <class Object>
    int Matrix<Object>::GetRow() const
    {
        return rows;
    }
    
    
    template <class Object>
    int Matrix<Object>::GetCol() const
    {
        return cols;
    }
    
    
    template <class Object>
    void Matrix<Object>::OutPut(ostream& out) const{
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; i++){
                cout << array[rows][cols] << " ";
            }
        cout << endl;
        }
    }
    Together with its interface:
    Code:
    #ifndef MATRIX_H
    #define MATRIX_H
    
    
    #include <iostream>
    #include <vector>
    
    
    using namespace std;
    
    
    template <class Object>
    class Matrix{
    public:
        Matrix(int row = 0, int col = 0);
        void setValue(Object value, int r, int c);
        Object GetValue (int r, int c) const;
        int GetRow() const;
        int GetCol() const;
        void OutPut(ostream& out) const;
    private:
        vector< vector<Object> > array;
        int rows;
        int cols;
    };
    
    
    #endif // MATRIX_H
    Using a driver programme, I cannot for the life of me instantiate the Matrix object:

    Code:
    #include <iostream>
    #include "matrix.h"
    
    
    using namespace std;
    
    
    int main(){
        Matrix<int> matrix(2, 2);
        return 0;
    }
    I have a compilation error that says undefined reference to 'Matrix<int>::Matrix(int, int)' and have no clue how to solve this.

    Any clues?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you realize that templates are usually header only implementations because both the definition and the implementation must be in the same compilation unit?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Things that will come up once you put the templates in the header file where they belong:
    Code:
    template <class Object>
    void Matrix<Object>::setValue(Object value, int r, int c)
    {
        array[r,c] = value;
    }
    Commas inside brackets probably aren't what you want here.

    Code:
    template <class Object>
    void Matrix<Object>::OutPut(ostream& out) const{
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; i++){
                cout << array[rows][cols] << " ";
            }
        cout << endl;
        }
    }
    You aren't using i and j inside your loop.

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by jimblumberg View Post
    Do you realize that templates are usually header only implementations because both the definition and the implementation must be in the same compilation unit?


    Ah thanks! Silly me. Im new to templates as you may see.

    Okay Ive just put it in a header only file but now Im getting an error printing out the Object:

    Code:
    #include <iostream>#include "matrix.h"
    
    
    using namespace std;
    
    
    int main(){
        Matrix<int> matrix(2, 2);
        matrix.OutPut(cout);
        return 0;
    }
    Code:
    #ifndef MATRIX_H
    #define MATRIX_H
    
    
    #include <iostream>
    #include <vector>
    
    
    using namespace std;
    
    
    template <class Object>
    class Matrix{
    public:
        Matrix(int row = 0, int col = 0);
        void setValue(Object value, int r, int c);
        Object GetValue (int r, int c) const;
        int GetRow() const;
        int GetCol() const;
        void OutPut(ostream& out) const;
    private:
        vector< vector<Object> > array;
        int rows;
        int cols;
    };
    
    
    template <class Object>
    Matrix<Object>::Matrix(int row, int col)
    {
        rows = row;
        cols = col;
        array.resize(row);
        for(int r = 0; r < row; r++){
            array[r].resize(col);
        }
    }
    
    
    template <class Object>
    void Matrix<Object>::setValue(Object value, int r, int c)
    {
        array[r][c] = value;
    }
    
    
    template <class Object>
    Object Matrix<Object>::GetValue(int r, int c) const
    {
        return array[r][c];
    }
    
    
    template <class Object>
    int Matrix<Object>::GetRow() const
    {
        return rows;
    }
    
    
    template <class Object>
    int Matrix<Object>::GetCol() const
    {
        return cols;
    }
    
    
    template <class Object>
    void Matrix<Object>::OutPut(ostream& out) const{
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                out << array[rows][cols] << " ";
            }
        out << endl;
        }
    }
    
    
    #endif // MATRIX_H
    The programme crashes at line 72 in the matrix.h file. I dont know whats causing it?
    Last edited by Vespasian_2; 10-23-2018 at 02:00 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    See! I told you they would come up.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Haha yeah I just saw your reply but I fixed that too it seems there is still the crashing error
    Last edited by Vespasian_2; 10-23-2018 at 01:58 PM.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    OH MY WORD EMBARRESING. Sorry you were right tabstop.

    There are no more errors

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    I feel like an idiot. You basically fixed the error before I asked the question, thats a first ive ever seen on a forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to instantiate derived class!!!
    By V8cTor in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2015, 09:52 AM
  2. Replies: 4
    Last Post: 11-15-2008, 06:11 PM
  3. instantiate unmanaged object in managed code
    By Redhead in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2006, 07:43 AM
  4. How can I make the base class unable to instantiate?
    By dxfoo in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 09:57 AM
  5. Using a templated object in another class
    By harry_p in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2002, 11:35 PM

Tags for this Thread