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

标签 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

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

最佳答案

看起来在main函数中,midiinmidioutRtMidiIn*RtMidiOut*类型(指向对象的指针),而 printMidiPorts 的参数是 RtMidiInRtMidiOut (对象)类型。看起来您需要做的就是更改 printMidiPorts 的签名。

关于c++ - 将 RtMidi 对象传递给函数 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8813540/

相关文章:

JavaScript 音序器 MIDI 文件时序分辨率 (PPQN)

ios - MIDINetworkConnection EXC_BAD_ACCESS 错误

c++ - 跨编译器共享内存?

c++ - 向 Windows 10 应用程序发送 BM_CLICK 消息不起作用

arrays - 修复char格式错误-Linux系统C程序

c - 为什么会出现这些错误?

ios - ios midi语音赞(0x90,30,127)

c++ - 字符串在转换为 char* C++ 时被截断

c++ - Boost Asio 回调不会被调用

从c中的数组语法计算地址