multi dimensional arrays and creating classes

hi,
I am sort of new to arduino, and i seem to be having problems with something that should be very simple.. Help!
The project is controlling an LED matrix in the form of a map, lighting an LED according to a particular event in that time and place in that map.

I am trying an array of object 'incident' with each one having 2 elements "time" (an int) and "place" (another int). I then want to declare the array and its elements and be able to get the time and place for each incident.
I created a class incident, with its getTime and getPlace functions, and i included the header file, all this seems fine. Then when i declare an array of incidents, i am getting errors, and not sure how to access each element.

Can anyone help? Am i making a mistake in the class? am i making a mistake in instantiating the variable in the main file? How do i proceed from here and access the elements of the array with thier variables?
Also, If such a class is not a good idea, should i do a two-dimensional array instead?

i am on a tight deadline, and would really appreciate the help,
thanks a lot,

ayah

Here is my header file:

/**
*create an incident class
*/

#ifndef incident_h
#define incident_h

// the #include statment and code go here...

class incident
{
public:
incident(int time, int place);
void getTime(void);
void getPlace(void);
private:
int _time;
int _place;
};

#endif

Here is my cpp file:


#include "incident.h"
incident::incident(int time, int place)
{
_time = time;
_place = place;
}
void incident::getTime()
{
}

void incident::getPlace()
{
}

, i am getting errors, and not sure how to access each element.

Can you copy the error messages, with the code they refer to ?

sure, when i declare a new array:

incident incidentTest[30];

i get the following:

error: no matching function for call to 'incident::incident()'/Applications/arduino-0011/hardware/libraries/incident/incident.h:13: note: candidates are: incident::incident(int, int)

/Applications/arduino-0011/hardware/libraries/incident/incident.h:11: note: incident::incident(const incident&)

In function 'void loop()':


And then, how do i do a for loop and access the value of each incident's time value?
ie:

for (indexCounter=0; indexCounter <= length of array; indexCounter++) // length of array?
Serial.print(incident[indexCounter].time); (//??)

thanks a lot

Short answer : AFAIK, you can't do it this way. Your class includes only one
constructor, which take parameters you can't provide in an array definition. You need to make an array of pointers, not an array of your class.

Long answer : ASAP :slight_smile:

Or just make your constructor do nothing and provide an init() method. I think then you'd be able to make an array of objects (though I haven't actually tried it).

  • Ben

Your class requires a constructor with parameters, which you can't provide in an array definition.

Actually, this code is an example that creates an array of four EchoServer objects the constructor of which takes a single parameter:

EchoServer servers[4] = {7, 8, 9, 10};

You need to make an array of pointers, not an array of your class.

The question of whether the OP actually wants/needs all the objects created at once is a valid one which would influence the best solution.

--Phil.

Or just make your constructor do nothing and provide an init() method. I think then you'd be able to make an array of objects (though I haven't actually tried it).

  • Ben

It works - this is the way I figured out how to do it when faced with the same issue and not knowing any other way to do it.

--Roy

Actually, this code is an example that creates an array of four EchoServer objects the constructor of which takes a single parameter:

EchoServer servers[4] = {7, 8, 9, 10};

That's good to know!

  • Ben

And just found out this would work with multiple parameters constructors :

      incident incidents[4] = {
            incident(1,1),
            incident(2,2),
            incident(3,3),
            incident(4,4)};

There does not seem to be a simplified notation for this.

Repeat with me, C arrays go from 0 to n-1.

{Not counting fixing any other errors.}

for (indexCounter=0; indexCounter <= length of array;  indexCounter++) // length of array? 
        Serial.print(incident[indexCounter].time); (//??)

should be

for (indexCounter=0; indexCounter < length of array;  indexCounter++) // length of array? 
        Serial.print(incident[indexCounter].time); (//??)