Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:13:55+00:00 2026-06-04T14:13:55+00:00

I am coding a matrix template class and the problem I am having is

  • 0

I am coding a matrix template class and the problem I am having is that the array is created and the values are filled but when I use the print() function, the array is empty again. Could anyone perhaps indicate what I’m doing wrong please?

data is defined as such:

T **data; 


template<class T> 
CMatrix<T>::CMatrix( int rows, int cols)
{
setRow(rows);
setCol(cols);

data = new int*[rows];
// Create matrix with dimensions given

for (int i = 0; i < row; i++) {
    data[i] = new int [cols];
}

for(int i = 0; i < row; i++) {
    for(int j = 0; j < cols; j++) {
        data[i][j] = (int) i * j;
    }
}
}


template<class T> 
void CMatrix<T>::print()
{
int i,j;

for (i=0;i<4;i++)
{ 
for(j=0;j<4;j++)
{
    printf("%.1f    ",data[i][j]);
}
printf("\n");
}
}

Main function:

int main (){    

int rows =4;
int cols = 4;

CMatrix<int>* matrixOne= new CMatrix<int>(rows, cols);
matrixOne->print();

    return(0);
}

Declarations of template:

template<class T> 
class CMatrix

{
private:
    int row;  // number of rows
    int column;  // number of columns

public:     

    CMatrix(int rows, int cols);

    CMatrix(const CMatrix& data); //Copy constructor

    //Constructor taking an array of 16 elements
    CMatrix(T Matrix[16]);
    ~CMatrix();

    T **data; 
    void setRow(int r);
    void setCol(int c);
    int getRow();
    int getCol();

    //Subscript operators
    T& operator()(int row, int col);
    T operator()(int row, int col) const;
    void print();



};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:13:57+00:00Added an answer on June 4, 2026 at 2:13 pm

    I would make sure you are using your data variable and your row and column variables in a class. This is by no means the most complete version, but it compiles and runs fine.

    Also make sure that you use “T” and not “int” in your constructor. See the comments.

    Note the two comments in the print function about using row and col instead of hard coding the values.

    #ifndef CMATRIX_H
    #define CMATRIX_H
    
    #include <stdio.h>
    
    template <class T>
    class CMatrix
    {
    public:
        CMatrix( int rows, int cols)
        {
            setRow(rows);
            setCol(cols);
    
            data = new T*[rows]; // replaced "int" for "T"
    
            for (int i = 0; i < row; i++) {
                data[i] = new T [cols]; // replaced "int" for "T"
            }
    
            for(int i = 0; i < row; i++) {
                for(int j = 0; j < cols; j++) {
                    data[i][j] = (T) i * j; // replaced "int" for "T"
                }
            }
        }
    
        void print();
        void setRow(int r){row = r;}
        void setCol(int c){col = c;}
        T& operator()(int row, int col);
    private:
        T **data;
        int row,col;
    };
    
    template <class T>
    void CMatrix<T>::print ()
    {
        int i,j;
    
        for (i=0;i < row;i++) // Here you used to have row hard coded to 4
        {
            for(j=0;j < col;j++) // Here you used to have col hard coded to 4
            {
                printf("%.1f    ",(float) data[i][j]);
            }
            printf("\n");
        }
    }
    
    // Recently added
    template<class T> T& CMatrix<T>::operator()(int row, int col)
    {
        return data[row][col];
    }
    
    #endif // CMATRIX_H
    

    Here is the main.

    #include "cmatrix.h"
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        CMatrix <float> m(4,4);
    
        m.print();
    
        // Recently added
        std::cout << m(1,1);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been implementing a custom template matrix class and I have a function I
I'm implementing a maxmin function, it works like matrix multiplication but instead of summing
I have a small problem in coding confusion 3x3 matrix by using matlab... I
When coding, I use Dropbox to sync folder in which I keep my projects
When coding with Eclipse, how do you turn off the * comments that come
I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat
# -*- coding: utf-8 -*- def present_unicode(list): for a in list: print u%s %
I'm using CUBLAS (Cuda Blas libraries) for matrix operations. Is possible to use CUBLAS
As part of a larger function, I'm writing some code to generate a vector/matrix
I am having some REAL big problems coding SVG XML and having it being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.