473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Array template

Hi,
I am trying to write a template for an array which I can use to
create multi-dimensional matrices as

// A 10x10 matrix with each element initialized to 0
Array< Array<int> > Matrix (10,0);

// A 5x5x5 3-dimensional matrix with each element initialized to 1
Array< Array <Array<int> > > Matrix3D (5,1);

// A 15x15x15x15 4-dimensional matrix with each element initialized to
10
Array< Array< Array< Array<int> > > > Matrix4D(15,10);

The code for the array class is as follows:

template < class T> class Array
{
private:
T* _data;
public:
Array(int Size, T Initializer)
{
int i;
_data= new T[Size];
for(i=0;i<Size;i++)
{
_data[i]=Initializer;
/************************************************** **
/***** OR SHOULD I USE
_data[i]=T(Initializer);
/************************************************** **/
}
}
};

Now if I try and declare a matrix as

Array< Array<int> > Matrix (10, 10 );

the compiler cribs as it expects an initializer of type Array<int> for
the Initializer when it does
_data[i]=T(Initializer);

Can someone please help me out ? how do I write a template for an
Array such that I can easily extend it to multidimensions
thanks a lot in advance.

Regards,
Madhu
Jul 19 '05 #1
2 6482
Now if I try and declare a matrix as

Array< Array<int> > Matrix (10, 10 );

the compiler cribs as it expects an initializer of type Array<int> for
the Initializer when it does
_data[i]=T(Initializer);

Try

Array <Array <int> > Matrix (10, Array <int> (10));

Regards,
Buster.
Jul 19 '05 #2
hrmadhu escribió:
I am trying to write a template for an array which I can use to
create multi-dimensional matrices as


I modified your code giving this:

#include <memory>

template <class T> class Array
{
private:
T * _data;
int s;
public:
Array(int Size, const T & Initializer)
{
s= Size;
int i;
_data= std::allocator <T> ().allocate (Size);
for(i=0;i<Size;i++)
{
new (& _data [i] ) T (Initializer);
}
}
Array (const Array & a)
{
_data= std::allocator <T> ().allocate (a.s);
for (int i= 0; i < a.s; ++i)
{
new (& _data [i] ) T (a._data [i] );
}
}
~Array ()
{
for (int i= 0; i < s; ++i)
{
_data [i].~T ();
}
std::allocator <T> ().deallocate (_data, s);
}
};

int main ()
{
// 10x10 matrix with values initialized to 1.
Array <Array <int> >
Matrix2D (10, Array <int> (10, 1) );
// 10x10x10 matrix with values initialized to 1.
Array <Array <Array <int> > >
Matrix3D (10, Array <Array <int> > (10, Array <int> (10, 1) ) );
}

Warning! I don't tested it more than compiling and executing the result.

You probably need to add an assignment operator to Array, or to forbid
the assignment.

Regards.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Metzen | last post by:
hello, ok, I want to find the length of an int array that is being passed to a function: main() { int array={1,2,3,4,5,6,7,8}; function(array); } function(int *array) {
14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
5
by: franklini | last post by:
i cant seem to figure out what is wrong with this class. it has to do with the input/output stream override. please can somebody help me. #include <iostream> #include <string> #include <vector>...
3
by: Kamran | last post by:
Hi I am quite new in this so please bear with me. I try to read some arrays of various types. I have defined a function: template <class T> T* getArray(int size, int el_size)
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
5
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.