Pages: [1]   Go Down
Author Topic: multi dimensional arrays and creating classes  (Read 1007 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()
{
}


Logged

Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

... could use some sleep

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-kitty
« Last Edit: June 08, 2008, 03:09:19 pm by tehboii » Logged

... could use some sleep

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:
Code:
EchoServer servers[4] = {7, 8, 9, 10};

Quote
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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 296
Got Karma?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Actually, this code is an example that creates an array of four EchoServer objects the constructor of which takes a single parameter:
Code:
EchoServer servers[4] = {7, 8, 9, 10};
That's good to know!

- Ben
Logged


Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And just found out this would work with multiple parameters constructors :
Code:
     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.
Logged

... could use some sleep

Central Florida, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

{Not counting fixing any other errors.}

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

should be

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

« Last Edit: June 09, 2008, 09:10:52 pm by kBit » Logged

Pages: [1]   Go Up
Jump to: