0

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 <jameslovescode@gmail.com>, 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
BugHunterUK
  • 6,765
  • 7
  • 41
  • 94
  • Ahhh. I thought it would be a simple issue. Please put as answer and I will accept. – BugHunterUK Mar 11 '17 at 14:17
  • My original suggestion was actually wrong, I tried to explain it a bit in my answer - comment there if anything is unclear – UnholySheep Mar 11 '17 at 14:28
  • 2
    It looks like you're a recovering Java programmer. C++ programmers avoid the word `new` as much as possible. (And the `Note notes[];` member isn't valid C++.) – molbdnilo Mar 11 '17 at 14:33
  • @molbdnilo Good guess, you're right. Do you have any resources that can explain this more and alternative programming practices to avoid using new all over the place? – BugHunterUK Mar 11 '17 at 14:43
  • Nevermind, found this http://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new – BugHunterUK Mar 11 '17 at 14:47
  • @BugHunterUK [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – molbdnilo Mar 11 '17 at 14:48

1 Answers1

1

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 Notes 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 deleteing 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 Notes 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 Notes be dynamically allocated) could be the have your Chord constructor create copies of the Notes 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 Notes 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);

UnholySheep
  • 3,125
  • 4
  • 20
  • 23