Array of ClassObjects

Hi all and exuse me if this topic can has the same argument of another one!

It's very important for me to ask you about a stupid thing.
I never use this forum for ask something, but just for reply and read about other problems...i always prefer to do thousands of tests after ask someting :(


I've create a template class called "TMatrix" for make a dynamic matrix and it works well.

But in the project i want to save any of the created matrix in an array, so my first idea was to create an array (in another non-template class) for collect any of the matrix created by the constructor.....result? thousands of errors that make me crazy :(

The specifics of the project says that i cannot use the standard syntax:

Class<T> Foo = new Class<T>();
because it seems like a Java code. I must use this for create an object:

Class<T> Foo[bar];
with "bar" the number of requested object....

I just want a dinamic array like Foo[bar] with the possibility of adding at runtime new objects!!! :(

sorry but i'm disperate...thanks all in advance
I welcome you to the world of std::vector
ahah...i forgot to mention that i can use only the <iostream> library :)
Hard to tell what is actually going wrong within your question. I picture you have something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

template <typename T>
struct PseudoMatrix
{
  T* data;

  PseudoMatrix( const int size )
    : data( new T[ size ] )
  {
  }

  ~PseudoMatrix( void ){ delete [] data; }
};

int main( void )
{
  PseudoMatrix<int> valid_matrix( 5 );
  PseudoMatrix<int> invalid_matrix[5]; // calls default constructor, which is undefined
  return 0;
}


which results in the error:
error: no matching function for call to ‘PseudoMatrix<int>::PseudoMatrix()


It is possible to define the array like so:
PseudoMatrix<int> matrix[] = { { 3 }, { 5 } };

It also sounds like you're trying to make your own vector. Chances are, you will want to add the default constructor to your matrix class, a copy-constructor, and a method to set the size of the matrix.
Last edited on
i've something like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <typename T> class TMatrix
{
     public:
          TMatrix();
          ~TMatrix();

          ........


     protected:
          .......
          T **M;
};

//approximately
template <typename T> TMatrix<T>::TMatrix()
{
     cout<<"Insert dimensions(m_rows n_columns):\n>";
     cin>>rows; cin>>cols;

     .......
   

     for(int i=0; i<rows;i++)
     {
		M[i] = new T[cols];
		for(int j=0; j<cols;j++)
		M[i][j]=(T) (i+j);
     }
}



I make my matrix using the double pointer technique and I just want to use another class to produce an array (called "MatrixSet") of pointers to TMatrix<T> type object!
In this array I can create at runtime new TMatrix<T> object and so, when i want to display the i-th matrix that i've create i just simply use this pseudocode:

 
MatrixSet[i].display();


I need to save in the array MatrixSet the matrix that i make at runtime so I can do with it some operation like addicion, subtraction, reverse, transposed, rank, .....
Last edited on
Topic archived. No new replies allowed.