Reputation: 8968
I'm getting the following error stating there's no matching function call:
no matching function for call to 'Chord::Chord(const char [5], Note* [3], int, int)'
I'm fairly new to C++ so I could be making an elementary mistake. But what I am trying to do is put notes on the heap, pass them to a constructor and have those notes copied to a private property within the Chord
class.
I can't seem to pinpoint why this is happening.
Inside main ...
Note *notes[] = {
new Note(0, "C", "B#"),
new Note(5, "E", "Fb"),
new Note(8, "G", "G")
};
Chord chord = new Chord("CMaj", notes, 127, 1);
Chord.h
/*
* Chord.h - Library for generating and playing chords
* Created by James Jeffery <[email protected]>, March 11, 2017.
*/
#ifndef Chord_h
#define Chord_h
#include "Arduino.h"
#include "Note.h"
class Chord
{
public:
Chord(String chord_name, Note notes[], int octave, int velocity);
String getChordName();
void play();
void stop();
private:
Note notes[];
String chord_name;
int octave;
int velocity;
};
#endif
Upvotes: 0
Views: 352
Reputation: 4096
The constructor is declared to accept an array of Note
, but Note *notes[]
declares an array of Note*
. As you stated in your question that you want to allocate the Note
s on the heap you should adjust your class to take Note *notes[]
in the constructor and store an array of pointers in its member.
However this solution can (and quite likely will) have issues with ownership and deallocation (who is in charge of delete
ing the allocated objects and when does it happen? And how to prevent any dangling pointer in that case?).
The IMO better approach would be to store the Note
s in automatic duration inside the Chord
class, so that they will be destroyed when the "owner" object gets destroyed. For this all you'd need to change would be the initial array you want to pass to:
Note notes[] = {
Note(0, "C", "B#"),
Note(5, "E", "Fb"),
Note(8, "G", "G")
};
Another option (if you really want to have the Note
s be dynamically allocated) could be the have your Chord
constructor create copies of the Note
s passed as a parameter, which could be allocated with new
and destroyed in the destructor of Chord
- this would solve the issues mentioned in the first solution (but it would disconnect the Note
s in Chord
from the ones passed in via the constructor)
Note: you also have another minor syntax error in your question: Chord chord = new Chord("CMaj", notes, 127, 1);
should be Chord* chord = new Chord("CMaj", notes, 127, 1);
Upvotes: 1