将RtMidi对象传递给函数(C ++)

时间:2012-01-11 02:46:31

标签: c++ pointers midi

在Processing中提出一些想法之后,我决定将我的MIDI项目转移到C ++以便移植到嵌入式平台。我已经决定将RtMidi库用于MIDI I / O,但是我在编写代码时遇到了一些麻烦。我对C ++还不是很了解。

基本上,我想将RtMidiIn对象和RtMidiOut对象传递给我的printMidiPorts函数(代码与RtMidi捆绑的一些示例代码相同)。我知道它与初始化midiin和midiout作为指针有关,但我不完全确定。

这是我的代码:

#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;
}

这是我的编译器输出:

    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

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

在main函数中,midiinmidiout的类型为RtMidiIn*RtMidiOut*(指向对象的指针),而参数为{{1}类型为printMidiPortsRtMidiIn(对象)。看起来您需要做的就是更改RtMidiOut的签名。

答案 1 :(得分:0)

您的功能签名错误。

这:void printMidiPorts(RtMidiIn midiin, RtMidiOut midiout) 将midiin和midiout声明为常规值,而不是指针。

void printMidiPorts(RtMidiIn *midiin, RtMidiOut *midiout)将是您功能的正确签名。