0

I'm not sure if this is more of an C++ question, but I've looked up both and still have no idea.

I have a sketch which controls 6 stepper motors using the AccelStepper library and in order to shorten the code, I would like to simply loop over them, or pass them to a function individually. They are all initialised with different parameters and then placed in an array.

However I get the error "conversion from ‘SomeClass()’ to non-scalar type ‘SomeClass’ requested" or "conversion from ‘SomeClass (*)()’ to non-scalar type ‘SomeClass’ requested" depending on what I try.

I've got experience with C and Java, not much C++ and I thought it would be a straightforward array of pointers, but I can't get the right combination.

I've made a cut down version that I try to compile with gcc to demonstrate either approach. I've removed all attempts at pointers with & or * to explain what I want:

SomeClass classA();
SomeClass classB();

// error: conversion from ‘SomeClass()’ to non-scalar type ‘SomeClass’ requested
SomeClass things[2] = {classA, classB};

int init(class SomeClass thing) {
    std::cout << "Setting up thing ";
    thing.setFoo(100);
}

int main() {

    for (int i=0; i < 2; i++) {
        things[i].setFoo(100);
    }

    // could not convert ‘classA’ from ‘SomeClass (*)()’ to ‘SomeClass’
    init(classA);

    return 0;
}

And just for completeness my class files which are simplified versions of the AccelStepper class:

SomeClass.h:

#include <stdlib.h>

class SomeClass
{
public:
    SomeClass();

    void    setFoo(float foo);
};

SomeClass.cpp:

#include <stdlib.h>
#include <string>
#include <iostream>
#include "SomeClass.h"

SomeClass::SomeClass()
{
}

void SomeClass::setFoo(float foo)
{
    std::cout << "foo ";
}

I hope someone doesn't mind explaining what is probably really simple and obvious! :)

1

There's a couple of fundamental errors in your sketch. Mainly your construction of your class instances:

SomeClass classA();
SomeClass classB();

Those are taken as functions that return a SomeClass object, not SomeClass constructors. Instead they should just read:

SomeClass classA;
SomeClass classB;

As for the rest, you are much better off using pointers to the classes - it makes passing them around so much easier. For instance, your array would look like this:

SomeClass *things[2] = {&classA, &classB};

That is an array of pointers to the objects. It just stores the addresses that they are at, rather than duplicating the objects into new objects within the array.

Your loop would then run like this:

for (int i=0; i < 2; i++) {
    things[i]->setFoo(100);
}

Note the use of -> instead of . because you are using a pointer to the object.

Similarly your init() function would be like this:

int init(SomeClass *thing) {
    Serial.println("Setting up thing");
    thing->setFoo(100);
}

init(&classA);

However, if you're never going to use the contents of your array with it you could use a reference instead of a pointer:

int init(SomeClass &thing) {
    Serial.println("Setting up thing");
    thing.setFoo(100);
}

init(classA);

That performs the same job as a pointer but leaves you with what looks like a real object rather than a pointer to the object - in fact it's just aliased the object name to a new one at the same address so you can still access it the same using .

  • Thanks for your answer! I should have included a more complicated constructor, the actual objects are created as eg: AccelStepper stepperA(AccelStepper::HALF4WIRE, motorPinA1, motorPinA3, motorPinA2, motorPinA4); Does this still hold with the above? – jbyrnes Sep 21 '15 at 6:03
  • From my C experience, this looks like a pointer to an array of pointers: SomeClass *things[2] = {&classA, &classB}; Is it different in C++? – jbyrnes Sep 21 '15 at 6:05
  • No, it's just an array [] of pointers *. If it was a pointer to an array of pointers it would be SomeClass **things[2] - exactly the same as in C. – Majenko Sep 21 '15 at 9:10
  • If your constructor takes parameters then you add parameters. If it doesn't you don't. It is a little confusing and I have no idea why they did it that way. – Majenko Sep 21 '15 at 9:11
  • @Majenko thanks for your answer. It has been a great deal of help. I was wondering, however, instead of having an array of pointers with references to class objects, couldn't we just have an array of these objects and reference them like myObj &obj = arr[idx];? – Phil Nov 5 '16 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.