473,503 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array using STL

hlg
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObjmyObjColumn;
typedef std::vector<myObjColumnmyObjRow;

....

myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;
}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

.... as I get the error message

"error C2008: '[' : unexpected in macro definition"
So, has anyone a solution ? (Or am I on the wrong track entirely ?)

hlg

--
Posted via a free Usenet account from http://www.teranews.com

Jun 14 '07 #1
7 3754

"hlg" <hu*@ga110n7744.freeserve.co.ukwrote in message news:46***********************@free.teranews.com.. .
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble.
Nah. Just means you haven't seen a reference to it.
I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];
That is non-standard nomenclature. Array elements in most
languages are Blat[7][5], not Blat[5,7]. I'm not sure your
desired syntax CAN be implimented in C++. You could easily
do Blat(5,7) using an application operator; but Blat[5,7] is
more problematic.
So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObjmyObjColumn;
typedef std::vector<myObjColumnmyObjRow;
Ewww. No. A row should be a vector of the objects you want
to store. The 2-D array itself is a (vertical) array of rows.
A "column" would be a slice, better done with valarray.
If you don't needs slices, then you can just use a vector of
vectors:

std::vector<std::vector<MyType MyArray;

Or better yet, use a template. Something like the following
(untested, off the top of my head) should work:

template<typename T>
class MyArrayType
{
public:
// Construct array:
MyArrayType(int width, int height)
{
repre_ = new std::vector<std::vector<T>* (height);
for (int i = 0; i < height; ++i)
{
repre_[i] = new std::vector<T(width);
}
}

// Destruct array:
~MyArrayType(int width, int height)
{
for (int i = height-1; i >= height; --i)
{
delete repre_[i];
}
delete repre_;
}

// Get reference to element of array:
T & operator()(int x, int y)
{
return representation_[y][x];
}
private:
// Underlying representation:
std::vector<std::vector<T>* >* repre_;
};

(The above is not guaranteed to be error free. You'll
have to debug and refine it yourself. But it should
give you an idea of a way to proceed.)
myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;
}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

... as I get the error message

"error C2008: '[' : unexpected in macro definition"
That's not a Microsoft issue. That's just the way the C
preprocessor is. Functionlike macros use parentheses, not
brackets.
So, has anyone a solution ? (Or am I on the wrong track entirely ?)
If you can use parentheses, Blat(5, 7), then yes. (see above.)

But if you insist on brackets, then I don't see a way to do that.
Maybe someone else here can see a way.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
Jun 14 '07 #2
On 14 Juni, 06:00, hlg <h...@ga110n7744.freeserve.co.ukwrote:
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble.
Read this and the following FAQs:
http://www.parashift.com/c++-faq-lit...html#faq-13.10
Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];
Sorry, but no can do, operator[] only takes one parameter, use
operator() or [][]-syntax.

--
Erik Wikström

Jun 14 '07 #3
On Jun 14, 8:18 am, Erik Wikström <eri...@student.chalmers.sewrote:
On 14 Juni, 06:00, hlg <h...@ga110n7744.freeserve.co.ukwrote:
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble.
Read this and the following
FAQs:http://www.parashift.com/c++-faq-lit...ing.html#faq-1...
Nevertheless, here it is:
I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];
Sorry, but no can do, operator[] only takes one parameter, use
operator() or [][]-syntax.
Well, his expression only gives it one argument. That's really
where the problem lies, and why you can't overload the way he
would like: myObjArray[x,y] has only a single index, the
expression "x,y", which evaluates x, throws out the result, then
evaluates y, and uses it. Because it is already so defined, you
can't redefine it without breaking code. One might argue that
such code deserves to be broken, but the committee doesn't like
to break code, even if it is bad code.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 14 '07 #4
On Jun 14, 8:00 am, hlg <h...@ga110n7744.freeserve.co.ukwrote:
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObjmyObjColumn;
typedef std::vector<myObjColumnmyObjRow;

...

myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;

}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

... as I get the error message

"error C2008: '[' : unexpected in macro definition"

So, has anyone a solution ? (Or am I on the wrong track entirely ?)

hlg

--
Posted via a free Usenet account fromhttp://www.teranews.com
typedef std::vector<myObjmyObjColumn;
typedef std::vector<myObjColumnmyObjRow;

....

myObjRow myObjArray;
myObjArray [x] [y] = myObjArray [x+1] [y]; /*double indexing needs two
braces not one enclosing a comma*/
regards,
FM.

Jun 14 '07 #5
hlg
terminator wrote:
On Jun 14, 8:00 am, hlg <h...@ga110n7744.freeserve.co.ukwrote:
>I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObjmyObjColumn;
typedef std::vector<myObjColumnmyObjRow;

...

myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;

}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

... as I get the error message

"error C2008: '[' : unexpected in macro definition"

So, has anyone a solution ? (Or am I on the wrong track entirely ?)

hlg

--
Posted via a free Usenet account fromhttp://www.teranews.com

typedef std::vector<myObjmyObjColumn;
typedef std::vector<myObjColumnmyObjRow;

...

myObjRow myObjArray;
myObjArray [x] [y] = myObjArray [x+1] [y]; /*double indexing needs two
braces not one enclosing a comma*/
regards,
FM.
Thanks all. To summarise:

The syntax I was looking for was [][] ... silly me ! (I'm a bit rusty)
.... and the double subscript operator can't be overloaded. Damn ! Never
mind. Press on.

hlg

--
Posted via a free Usenet account from http://www.teranews.com

Jun 14 '07 #6
On Jun 15, 1:08 am, hlg <h...@ga110n7744.freeserve.co.ukwrote:
>
#define myObjArray[x,y] GetMyObj(x,y)
... as I get the error message
"error C2008: '[' : unexpected in macro definition"
I think this is a compiler bug; the above should
define an object-like macro, replacing the string
'myObjArray' with '[x,y] GetMyObj(x,y)'. The
specification of object-like macros doesn't seem
to require whitespace between the macro name and the
replacement text.
The syntax I was looking for was [][] ... silly me ! (I'm a
bit rusty)and the double subscript operator can't be overloaded.
However you can overload [] to return a custom class
that itself overloads [], getting the same effect.

Jun 14 '07 #7
On Jun 15, 3:39 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Jun 15, 1:08 am, hlg <h...@ga110n7744.freeserve.co.ukwrote:
>#define myObjArray[x,y] GetMyObj(x,y)
>... as I get the error message
>"error C2008: '[' : unexpected in macro definition"

I think this is a compiler bug; the above should
define an object-like macro, replacing the string
'myObjArray' with '[x,y] GetMyObj(x,y)'. The
specification of object-like macros doesn't seem
to require whitespace between the macro name and the
replacement text.
The syntax I was looking for was [][] ... silly me ! (I'm a
bit rusty)and the double subscript operator can't be overloaded.

However you can overload [] to return a custom class
that itself overloads [], getting the same effect.
double parametrized operator() could be overloaded as well but someone
may prefer an indexer class as you pointed out:

struct array{
class value_type;
...
...
friend class indexer;
class indexer{
value_type & operator[](unsigned yyy);//[x] [yyy]
...
};

indexer & operator[](unsigned xxx);//[xxx] [y]

value_type & operator()(unsigned,unsigned);//(x,y)==[x][y]

};

regards,
FM.
Jun 19 '07 #8

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

Similar topics

12
55526
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10198
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
9
4765
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
35
6589
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
4431
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
204
12886
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
21
3168
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
23
7364
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
2092
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.