简体   繁体   中英

Passing RtMidi objects to functions (C++)

After laying out some ideas in Processing, I've decided to move my MIDI project to C++ for portability to an embedded platform. I've decided to use the RtMidi library for MIDI I/O but I'm having some troubles laying out the code how I want it. I'm not great with C++ yet.

Basically, I want to pass the RtMidiIn object and RtMidiOut object to my printMidiPorts function (the code is the same as some example code bundled with RtMidi). I understand that it has something to do with initializing the midiin and midiout as pointers, but I'm not totally sure.

This is my code:

#include <stdio.h>
#include <iostream>
#include <string>
#include "rtmidi/RtMidi.h"

using namespace std;

void printMidiPorts(RtMidiIn midiin, RtMidiOut midiout)
{
    // Check inputs.
    unsigned int nPorts = midiin->getPortCount();
    std::cout << "\nThere are " << nPorts << " MIDI input sources available.\n";
    std::string portName;
    for ( unsigned int i=0; i<nPorts; i++ ) {
        try {
            portName = midiin->getPortName(i);
        }
        catch ( RtError &error ) {
            error.printMessage();
            goto cleanup;
        }
        std::cout << "  Input Port #" << i+1 << ": " << portName << '\n';
    }

    // Check outputs.
    nPorts = midiout->getPortCount();
    std::cout << "\nThere are " << nPorts << " MIDI output ports available.\n";
    for ( unsigned int i=0; i<nPorts; i++ ) {
        try {
            portName = midiout->getPortName(i);
        }
        catch (RtError &error) {
            error.printMessage();
            goto cleanup;
        }
        std::cout << "  Output Port #" << i+1 << ": " << portName << '\n';
    }
    std::cout << '\n';

    // Clean up
    cleanup:
    delete midiin;
    delete midiout;

}

int main ()
{

    RtMidiIn  *midiin = 0;
    RtMidiOut *midiout = 0;

    // RtMidiIn constructor
    try {
        midiin = new RtMidiIn();
    }
    catch ( RtError &error ) {
        error.printMessage();
        exit( EXIT_FAILURE );
    }

    // RtMidiOut constructor
    try {
        midiout = new RtMidiOut();
    }
    catch ( RtError &error ) {
        error.printMessage();
        exit( EXIT_FAILURE );
    }

    printMidiPorts(midiin, midiout);

    return 0;
}

And this is my compiler output:

    lightArray.cpp: In function ‘void printMidiPorts(RtMidiIn, RtMidiOut)’:
    lightArray.cpp:19: error: base operand of ‘->’ has non-pointer type ‘RtMidiIn’
    lightArray.cpp:24: error: base operand of ‘->’ has non-pointer type ‘RtMidiIn’
    lightArray.cpp:34: error: base operand of ‘->’ has non-pointer type ‘RtMidiOut’
    lightArray.cpp:38: error: base operand of ‘->’ has non-pointer type ‘RtMidiOut’
    lightArray.cpp:50: error: type ‘class RtMidiIn’ argument given to ‘delete’, expected pointer
    lightArray.cpp:51: error: type ‘class RtMidiOut’ argument given to ‘delete’, expected pointer
    lightArray.cpp: In function ‘int main()’:
    lightArray.cpp:79: error: conversion from ‘RtMidiIn*’ to non-scalar type ‘RtMidiIn’ req

Any help is much appreciated. Thanks!

It looks like in the main function, midiin and midiout are of types RtMidiIn* and RtMidiOut* (pointers to objects), while the parameters to printMidiPorts are of types RtMidiIn and RtMidiOut (objects). It looks like all you need to do is change the signature for printMidiPorts .

Your function signature is wrong.

this: void printMidiPorts(RtMidiIn midiin, RtMidiOut midiout) declares midiin and midiout as regular values, not pointers.

void printMidiPorts(RtMidiIn *midiin, RtMidiOut *midiout) would be the correct signature for your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM