March 28, 2024, 12:04:44 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


can I make an array of my vector objects?

Started by John S, January 05, 2007, 02:48:13 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

John S

Paul,
I tried this code and it compiles, but crashes.  Can you point me in the right direction?  Do I need to use the GlobalAlloc stuff for this?



// dVector_test.src
// test program for DVector class
// by John Siino  (01/05/2007)

#include "dVector.inc"

sub main(),int
{
DVector A[3];
  for x = 0; x < 3; x++
  {
A[x].Resize(3);
}
  for x = 0; x < 3; x++
  {
for int i = 0; i < 3; x++
  {
    A[x].Set(i, RND(100));
}
}

  for x = 0; x < 3; x++
  {
    A[x].Write("Original Vector A[", "numtostr(x,0)", "] = ", 3 );
    writeln("\n\n");
}

  print("Angle between Vectors:  A[0] and A[1] is ", A[0].AngleD( A[1] ), " degrees\n\n");

writeln("\n\n");
  writeln("Press any key to continue\n");
  while GetKey() = "";
writeln("\n\n");

return 0;
}

John Siino, Advanced Engineering Services and Software

LarryMc

John
Quotefor x = 0; x < 3; x++
     {
         for int i = 0; i < 3; x++
           {
             A
  • .Set(i, RND(100));
                }
          }

I'm not sure but it looks to me like you're incrementing x in 2 places inside one for loop.
Shouldn't  "for int i = 0; i < 3; x++" really be "for int i = 0; i < 3; i++"
That inner for loop just looks wrong to me.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

That and arrays of classes won't be properly constructed.  Only the first element of the array will be.  I've mentioned that before ;)

Ionic Wind Support Team

John S

D'oh!
It was late.  Thanks for the feedback Larry & Paul.

Paul what would it take to allow an array of classes?  Is that a useful concept?
John Siino, Advanced Engineering Services and Software

Ionic Wind Support Team

January 05, 2007, 11:49:15 AM #4 Last Edit: January 05, 2007, 01:46:37 PM by Paul Turley
You could use a list of classes instead.  Right now the compiler doesn't directly support arrays of classes, although you can make it work if you manually call the constructor for each element > 0 and then call the destructor for each element > 0 when you are done with the array.

The constructor has the same name as the class, so ...

for x = 1; x < 3; x++
{
A[x].DVector();
}

Would call the constructor for each element of the array, except the first which is called by the compiler and...

for x = 1; x < 3; x++
{
A[x]._DVector();
}

Would call the destructors when you are finished.

Paul.
Ionic Wind Support Team

LarryMc

Yeah... that's right... Paul
That's exactly what i WAS GOING TO TELL HIM TO DO! ;D
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

Edited my post since the forum was changing the array symbol to a square block  ::)  That'll teach me to use code blocks ;)
Ionic Wind Support Team

John S

Awesome, I'm going to try it on my Vector Math stuff I'm doing. 

BTW, I found some stuff regarding "Multi-dimensional vector product" by Z. K. Silagadze of Budker Institute of Nuclear Physics, Novosibirsk, Russia.  I also found material on Wikipedia.  I'm going to see if I can apply the information gleaned in my Aurora Vector Math module.

I would also like to know how to use Aurora stuff (via dll or directly coded in) in Ebasic.  Does the GlobalAlloc stuff work in the same way in EBasic?
John Siino, Advanced Engineering Services and Software

mrainey

QuoteThat's exactly what i WAS GOING TO TELL HIM TO DO!


You too?
Software For Metalworking
http://closetolerancesoftware.com

John S

January 05, 2007, 06:02:47 PM #9 Last Edit: January 05, 2007, 06:16:05 PM by John S
Quote from: Paul Turley on January 05, 2007, 11:49:15 AM
You could use a list of classes instead.  ... if you manually call the constructor for each element > 0 and then call the destructor for each element > 0 when you are done with the array.

Should the destructor be called in decreasing order or does it not matter?

for x = 2; x >0; x--
{
   A [ x ] . _DVector();
}
John Siino, Advanced Engineering Services and Software

Ionic Wind Support Team

Doesn't matter as you are calling the destructor of individual classes, not derived.
Ionic Wind Support Team