Menu

A class whose objects have variable size

2008-09-03
2012-09-26
  • Chris Hammond

    Chris Hammond - 2008-09-03

    Hi,

    I am using DEV C++ 4.9.9.2 on Windows XP. I'm trying to define a matrix class, on which I can then define matrix multiplication and member functions that return its eigenvalues etc. Let me point out in advance that I realize this should probably be a template and that there are better ways of constructing a matrix class using the standard template library. However, I'm doing this for practice, so please ignore these facts for the moment.

    For the moment, I just want to define a matrix class as follows:

    include <cstdlib>

    include <iostream>

    include<cmath>

    using namespace std;

    class matrix {
    public:
    static int width;
    static int height;
    float MatrixArray[width][height];

    } ;

    int main(int argc, char *argv[])
    {

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
    

    }

    It seems like the problem is that the size of the array is not predetermined. I don't want to fix some maximum size in advance. Is there a way around this by making the member a pointer to an array?

    Here is the error I get when I try to compile this.

    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Getting Started\Matrix1\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\Getting Started\Matrix1\Makefile.win" all
    g++.exe -c MatrixMain1.cpp -o MatrixMain1.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -I"C:/Dev-Cpp/joshi"

    MatrixMain1.cpp:13: error: data member may not have variably modified type `float[((unsigned int)((int)matrix::width))][((unsigned int)((int)matrix::height))]'

    make.exe: *** [MatrixMain1.o] Error 1

    Execution terminated

    Thanks,

    Chris

     
    • Anonymous

      Anonymous - 2008-09-03

      Ooops: I forget to init the properties and other minor mistake:

      class matrix {
      public:
      int width; // columns
      int height; // files
      float** MatrixArray;

      matrix (int w =0, int h =0) : width(w), height(h) {
      MatrixArray = new float* [h];
      for (int i = 0; i < h; i++) {
      MatrixArray[i] = new float [w];
      }
      }

      ~matrix() {
      for (int i = 0; i < height; i++) {
      delete[] MatrixArray[i];
      }
      delete[] MatrixArray;
      }
      } ;

       
    • Anonymous

      Anonymous - 2008-09-03

      What about this design to Your class:

      class matrix {
      public:
      int width;
      int height;
      float** MatrixArrayPtr;

      matrix (int w =0, int h =0) {
          MatrixArrayPtr = new float* [h];
          for (int i = 0; i &lt; h; i++) {
              MatrixArrayPtr[j] = new float [h];
         }
      }
      

      ~matrix() {
      for (int i = 0; i < height; i++) {
      delete[] MatrixArrayPtr[i];
      }
      delete[] MatrixArrayPtr;
      }
      } ;

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.