2012-01-11 195 views
0

在處理中闡述了一些想法之後,我決定將我的MIDI項目移動到C++以便移植到嵌入式平臺。我決定使用RtMidi庫進行MIDI I/O,但是我有一些麻煩佈置代碼,我想要它。我用C++還不是很棒。將RtMidi對象傳遞給函數(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 

任何幫助深表感謝。謝謝!

回答

1

它看起來像在主函數,midiinmidiout是類型RtMidiIn*RtMidiOut*(對象指針),而參數printMidiPorts是類型RtMidiInRtMidiOut(對象)。它看起來像你需要做的是改變簽名printMidiPorts

+0

太棒了。工作完美 - 謝謝。 – distorteddisco 2012-01-11 03:45:35

0

你的函數簽名是錯誤的。

這個:void printMidiPorts(RtMidiIn midiin, RtMidiOut midiout) 聲明midiin和midiout爲常規值,而不是指針。

void printMidiPorts(RtMidiIn *midiin, RtMidiOut *midiout)將是您的功能的正確簽名。