Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've created 1 library with 2 classes. Class Wave and Class LEDLamps. In the second class constructor i'm trying to populate a array of first class objects without any luck.

Here are some parts of my real code. h file:

static const int numberOfWaves = 20;

class Wave
{
public:
    Wave(int speed, int blockSize, int ledCount, int lightness,int startCount); // Constructor

private:

};

// ------------------------------------------------------------------------------------------- //
class LEDLamps
{
public:
    LEDLamps(int8_t lampCount, int8_t dataPin, int8_t clockPin); //Constructor

private:
    Wave waveArray[numberOfWaves];
};

.cpp file

Wave::Wave(int speed, int blockSize, int ledCount, int lightness, int startCount) //Constructor
{ 
           // Doing some stuff...
}

// ------------------------------------------------------------------------------------------- //
LEDLamps::LEDLamps(int8_t lampCount, int8_t dataPin, int8_t clockPin) //Constructor
{ 
    int i;
    for (i = 0; i < numberOfWaves; i++) {
        waveArray[i] = Wave(10,2,25,150,100);
    }
}

Error messages:

LEDLamps.cpp: In constructor 'LEDLamps::LEDLamps(int8_t, int8_t, int8_t)':
LEDLamps.cpp:66: error: no matching function for call to 'Wave::Wave()'
LEDLamps.cpp:14: note: candidates are: Wave::Wave(int, int, int, int, int)
LEDLamps.h:23: note:                 Wave::Wave(const Wave&)

What i understand from that error message the parameters are wrong but i'm sending 5 integer and constructor is defined to receive 5 integer? So i must be something else i'm doing wrong...

share|improve this question

1 Answer 1

The error tells you exactly what wrong, there is no Wave::Wave() method. You need a default constructor for the Wave class to be able to create an array of it. You might also want to create a copy-assignment operator if the Wave class contains non-trivial data.

The problem is that the array is constructed before the body of the LEDLamps constructor runs, so when inside the LEDLamps constructor body the array is fully constructed, and what you are doing is assignment (using the automatically generated copy-assignment operator).


Unfortunately the default Arduino C++ library it quite limited, at least when it comes to "standard" C++ features. There are libraries that helps, and if it's possible to use such a library you could have used an std::vector instead, which would allow you to construct the vector in the constructor initializer-list:

class LEDLamps
{
    ...
    std::vector<Wave> waveVector;
};

...

LedLamps::LEDLamps(...)
    : waveVector(numberOfWaves, Wave(10,2,25,150,100))
{
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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