gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:11:56 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com