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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:10:30+00:00 2026-05-28T05:10:30+00:00

I have two method : template <class T> Matrix<T>::Matrix(int rows, int cols, T* data)

  • 0

I have two method :

template <class T>
Matrix<T>::Matrix(int rows, int cols, T* data)

{
 this->nRows = rows;
 this->nCols = cols;

for (int i=0; i < nRows; i++)
{

  vector<T> col(nCols);
     for(int j=0;j<nCols;j++)

      col[j]=*(data+i*nCols+j);

   m.push_back(col);

}

}

And for print the matrix:

  template <class T>
  void Matrix<T>::Dump(void)
  {
    cout << "\t[\n";
    for (int row=0; row<nRows; row++)
    {
     for (int col=0; col<nCols; col++)
      cout << "\t\t" << m[row][col] << " ";
     cout << "\n";
    }
     cout << "\t]\n";
   }

And the test case is :

int M[]={1,2,3,3,4,7,2,5,8};
Matrix<int> m(3,3,M);
m.Dump();

Scenarios(In constructor) are :

  for(int j=0;j<nCols;j++)
     {
      T val(*(data+i*nCols+j));
      col.push_back(val)  ;
        cout<<col[j];
     }

The output is all 0.

But

for(int j=0;j<nCols;j++)
     {
      T val(*(data+i*nCols+j));
      col[j]=val ;
        cout<<col[j];
     }

Gives the correct result.

Please explain why pushback behaves different in both cases ?

  • 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-28T05:10:30+00:00Added an answer on May 28, 2026 at 5:10 am

    This

     vector<T> col(nCols);
    

    line already contracts a column with all zeros in it.
    In the first case you push back new value and get something like this

    [0, 0, 0, 1, 2, 3]
    

    as the first column. So it is not surprising that

    cout<<col[j];
    

    outputs zeros, because j is not bigger than 2!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method and two classes defined like this: template<template<class X> class T>
I have these two methods on a class that differ only in one method
I have two classes, point and pixel: class point { public: point(int x, int
I have a defined a simple template class. In this class I define a
assume that I have the following two template classes : template <class _A> class
I have a class called 'subscribedQueue'. This class receives its data via its subscribed
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have this class which has a double list template of a struct of
I have a template class, and would like to write a member method that's
I have two implementations of a method, one for value types and another for

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.