how to define an array of class objects

06 Nov 2010

DS1820 probe1(p27, p28);

const int MAX_PROBES = 16;
DS1820 probe[MAX_PROBES](p27,p28);

The first line works just fine. But, how do I define an array of class objects?
I get this error
"A value of type "PinName" cannot be used to initialize an entity of type "DS1820 [16]" (E144)" in file "/main.cpp"
Here's the reference to PinName
DS1820::DS1820 (PinName dp, PinName pp) : _datapin(dp), _parasitepin(pp) {

06 Nov 2010

You are trying to attach more than one object to the same pair of pins?

06 Nov 2010

Yes, but it works without the array

 

DS1820 probe1(p27, p28);
DS1820 probe2(p27, p28);

06 Nov 2010

The form of what you are trying to do is:

DS1820 probe[MAX_PROBES] = {probe(p27,p28), probe(p27,p28), … for each member in the array}

But this assumes you know at coding time how many class object members are going to be in the array and you seem to want the number to be dynamic.

You could take the initialization out of the constructor and call a member function to set the values:

DS1820 probe[MAX_PROBES];

...

 

for (int i = 0; i < MAX_PROBES; i++)
     probe[i].Set_Pins(p27,p28);

...

 

Where Set_Pins is an additional member function to set the pin numbers instead of the constructor.

You could also write a macro, but you probably should just use an array of class object pointers instead of actual objects, unless there is some memory management issue:

const int MAX_PROBES = 16;
DS1820* probe[MAX_PROBES];

 

 

for (int i = 0; i < MAX_PROBES; i++)
     probe[i] = new DS1820(p27,p28);

 

 

Harry 

 

 

 

 

 

 

 

06 Nov 2010

I like this route: Although the number of probes is unknown, I have no problem defining the array with a fixed maximum number of probes. There's only about 20 bytes of data per probe.

const int MAX_PROBES = 16;
DS1820* probe[MAX_PROBES];

 

 

// I placed this inside of main{}
for (int i = 0; i < MAX_PROBES; i++)
probe[i] = new DS1820(p27,p28);

Next question: how do I reference the class functions? I'm getting "Expression must have class type (E153)". I tried a few different things. I'll also admit I'm not the best with pointers, I'm just getting back into C programming.

probe[i].temperature('f')

FYI: This is for a Dallas OneWire temperature probe. I first wrote straight code to get it working, then converted it into a class. Now I'm putting together some code examples to go with the library. I'll publish the completed works.

If you'd like I could email you the code, so you can play and compile. Or I think I saw someone mention a way to share the code on the mbed site. Just tell me how.

07 Nov 2010

probe[i]->temperature('f')

If you e-mail me the code, I'd be happy to help.

Harry

Harry_Wahl@Hotmail.com

07 Nov 2010

That compiled just fine. And even seems to be running my code correctly.

I'll try to finish up the examples and post the library.

You've been very helpful.

I had a previous question at
http://mbed.org/forum/mbed/topic/1197/?page=1#comment-5808

The program compiles, but seems to lock up when I run it. I'll post an update to that question too keep the question threads.