By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 2,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Array using STL

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.