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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:12:50+00:00 2026-06-13T04:12:50+00:00

I need help in something really simple, which C++ makes very difficult. I created

  • 0

I need help in something really simple, which C++ makes very difficult. I created a class, lattice, with the aim to initialize and treat a matrix. There are the following private members:

private unsigned dim;
private double matrix [dim][dim];

I would like to initialize the variable dim in the constructor of the class through a parameter, but the compiler continue to return errors. I tried to make dim public and static and initializing it in the main program, but there are still problems. How could I create this simple class?

Moreover, I also implemented some methods in the class in order to update the values of the matrix. Is it true that, initializing an object of the class in a main program and then using its “updating” methods, the values of the matrix are stored only once?

Thank you for your help.

  • 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-13T04:12:51+00:00Added an answer on June 13, 2026 at 4:12 am

    There are (at least) three ways to create such a class:

    • with a template

      template<size_t dim>
      class Matrix
      {
         private:
           double matrix[dim][dim];
      }
      
    • with the use of built in types such as std::vector (e.g. valarray would work too)

      #include <vector>
      class Matrix
      {
         private:
           size_t dim;
           std::vector<std::vector<double> > matrix;
         public:
           Matrix(size_t dim_) : dim(dim_), matrix()
           {
             matrix.resize(dim);
             for ( size_t i = 0; i < dim; ++i )
                matrix[i].resize(dim);
           }
      }
      
    • with the use of plain arrays (I do not recommend that!)

      class Matrix
      {
         private:
           size_t dim;
           double** matrix;
         public:
           Matrix(size_t dim_) : dim(dim_), matrix()
           {
             matrix = new double*[dim];
             for ( size_t i = 0; i < dim; ++i )
                matrix[i] = new double[dim];
           }
           ~Matrix() // you need a destructor!
           {
             for ( size_t i = 0; i < dim; ++i )
                delete [] matrix[i];
             delete [] matrix;
           }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little help with (probably) something really simple. I want to use
Hello i am new in android and i really need help with something. I
I need some help with something... say I have the following form... <form name=
I need a little help clearing something up with Zend_Form and adding filters to
This is something really simple, but I can't seem to find the answer anywhere,
I have a form field in Drupal which I need some help with. The
I've spent over 6 hours on this issue by now and really need help.
I need help with what I thought was going to be simple local machine
Ok, so I've pretty much tried everything. I bet it's something really simple but
I really need help. I am making a game app for my final year

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.