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 6537387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:36:38+00:00 2026-05-25T10:36:38+00:00

Here is a matrix template I recently started implementing. It doesn’t have many functions

  • 0

Here is a matrix template I recently started implementing. It doesn’t have many functions since, like I said, I just started it.

Header file:

#ifndef MATRIXX_H
#define MATRIXX_H

#include <iostream>
using namespace std;

template <typename theType> class Matrix
{
public:
Matrix()
{
    rows = columns = 0;
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }

}

Matrix(int rows, int columns) 
{
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }
}

~Matrix()
{
    delete [] *matrixRoot;
    delete [] matrixRoot;
}

int numrows() const
{
    return rows;
}

int numcols() const
{
    return columns;
}

private:
int rows;   
int columns;    
theType **matrixRoot;

};

#endif

The problem arises when I try to run this testing program:

#include "matrixx.h"
#include <iostream>

void TestingMatrix()
{
int r = 5;
int c = 4; 
Matrix<int> a(r, c);

cout << "Number of rows: " << a.numrows() << " Number of columns: " << a.numcols() << endl;
}

int main()
{
TestingMatrix();
}

Should it be correct, I’d expect the output to yield something like:

Number of rows: 5 Number of columns: 4

Instead I get something way off:

Number of rows: 134515193 Number of columns: 2515748

It’s interesting to note that the number of columns changes but not the rows each time the program is run.

Obviously I won’t be able to get anywhere if I can’t even initialize an instance of the matrix correctly, so I’d like to know what it is I’m doing wrong that gives me such erroneous results. Btw, I’m aware that my destructor’s also messed up since it causes the program to segfault (lol) but that’s a problem I plan on addressing on my own in the future. I’d really just appreciate if someone could explain why I’d get those numbers for row and column and how I can get it to return the correct values.

Thank you. (:

  • 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-05-25T10:36:38+00:00Added an answer on May 25, 2026 at 10:36 am
    Matrix<int> a(r, c);  // Invokes constructor with arguments.
    

    You are not initializing the member variables rows and columns present in the two argument constructor. And so getting some garbage values from their getters.

    Matrix(int rows, int columns) : rows(rows), columns(columns)
                                   // ^^^^^^^^^^^^^^^^^^^^^^^^^ Add this
    {
           // ....
    }
    
    • 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
Here's a problem I ran into recently. I have attributes strings of the form
Here is my question : I have A = 1xN matrix and B =
Here is an example: A matrix like [,1] [,2] [1,] 1 4 [2,] 2
I have a problem with generics in c++.I have two Matrix.h and Matrix.cpp files.Here
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here we go again, the old argument still arises... Would we better have a

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.