## Need help with multi-dim arrays pleae

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### Need help with multi-dim arrays pleae

In a header file of one of my classes I have a private two dim array,
Code: Select all
`double V[][6] = {      {-.01,0,0, -.2,-.2,1},  // POINT AND NORMAL      {.1,0,0, .2,-.2,1},      {0,.11,0,  -.2,.2,1},      {.08,.09,0,  .2,.2,1}   };`

but this gives me a compiler error. It says "syntax error : '{'" and
"unexpected token(s) preceding '{'; skipping apparent function body".
Does anyone know why? All I want to do is declare and initialize it.

Also, lets say I have a 3-dim array, (which I cant initialize either until I fix my first problem)
Code: Select all
`double polygon[][][] = {      { V[0], V[1], V[2] },      { V[1], V[2], V[3] }   };`

and I need a for loop to go thru polygon. I want to do something like:
Code: Select all
`for (int n = 0 ; n < polygon.length ; n++)  for (int i = 0 ; i < polygon[n].length ; i++)    for (int k = 0 ; k < 3 ; k++)       normal[k] = polygon[n][i][k+3];`

What do I replace the polygon.length's with? Would it be sizeof(polygon)/sizeof(polygon[0]) for the outer loop and sizeof(polygon[0])/sizeof(polygon[0][0]) for the inner?

This stuff confuses me.

Thanx for the help,
-Flack
Flack

First question: You cant assign values in the object, you must do that trouhgt a constructor.

correct me if im wrong

Zen

Posts: 1088
Joined: Wed Sep 24, 2003 1:41 am
Location: Norway

The correct way to do this is writing a class Point and a class Vector, each of which holds three doubles, a class Point_and_normal that holds a Point and a Vector, a class Polygon that holds a std::vector of `Point_and_normal's.

That design will make your life easier. The code you were asking about would look like this:

Code: Select all
`std::vector<Polygon> polygons;for(int n=0; n<polygons.size();++n){  Polygon const &p = polygons[n];  for(int i=0; i<p.vector.size(); ++i)    normal = p.vector[i].normal;}`

Post again if you can't figure out how to write the classes, or if for some reason you don't like this approach.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Thanx for the help guys.

Alvaro, could you show me the correct way to write the classes please?

-Flack
Flack

Code: Select all
`struct Point {  double coords[3];  Point(double x, double y, double z){    coords[0]=x; coords[1]=y; coords[2]=z;  }};struct Vector {  double coords[3];  Vector(double x, double y, double z){    coords[0]=x; coords[1]=y; coords[2]=z;  }};struct Point_and_normal {  Point point;  Vector normal;  Point_and_normal(Point const & p, Vector const &n):point(p),normal(n){}};struct Polygon {  std::vector<Point_and_normal> vector;  Polygon(std::vector<Point_and_normal> const &v):vector(v){}};`

Most people don't implement a class Point, and they use Vector for points instead. In what I have written here, it makes no difference. I would still define them separately, because they are not the same thing and some operations make sense only with one of the types (addition of points doesn't make sense, for instance). But feel free to go with the crowd and use Vector for everything.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Thanx for the help.

I have a question though. What does this line do:
Code: Select all
`Point_and_normal(Point const & p, Vector const &n):point(p),normal(n){}`

similarly
Code: Select all
`Polygon(std::vector<Point_and_normal> const &v):vector(v){} `

Are these lines declaring functions in the struct? Im not familiar with the syntax. (What is the colon there for?)

Thanx,
-Flack
Flack

Flack wrote:Thanx for the help.
Are these lines declaring functions in the struct? Im not familiar with the syntax. (What is the colon there for?)

Those are constructors, and the colon starts an initializer list. You can read about these things in any C++ book.

The idea is that now you can construct a Point_and_normal like this:
Code: Select all
`Point P(0.0, 1.0, 0.0);Vector N(1.0, 0.0, 0.0);Point_and_normal Pn(P,N);`

or even
Code: Select all
`Point_and_normal Pn(Point(0.0,1.0,0.0), Vector(1.0,0.0,0.0));`

The examples with vectors are a little bit more complicated, but the idea is the same.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA