簡體   English   中英

無法從int函數返回1或0以外的任何值

[英]Can't return anything other than 1 or 0 from int function

我希望我的第一篇文章不是那么新手。 到目前為止,我一直在使用openframeworks,但由於我是編程新手,所以我真的很頭疼,要從int函數中返回正確的值。 我希望int遞增直到滿足布爾條件,然后遞減為零。 int用於在數組中從頭到尾再返回。 當我把函數的勇氣放到我使用int的方法中時,一切工作正常,但是非常混亂,我想知道放置在那里的計算量有多昂貴,似乎我的語法能力似乎不足除此以外。 意見表示贊賞,並在此先感謝。

int testApp::updown(int j){

if(j==0){
    arp =true;

}
else if (j==7){
    arp = false; 

}

if(arp == true){
    j++;

}

else if(arp == false){
    j--;

}

    return (j);

}

然后在我正在使用的庫的audioRequest塊中這樣調用:

for (int i = 0; i < bufferSize; i++){


 if ((int)timer.phasor(sorSpeed)) {

            z = updown(_j);
            noteOut = notes [z];

            cout<<arp;
            cout<<z;

        }

編輯:對於一些信息的補充。 刪除了第二個if語句的最后一個條件,它在那里是因為我遇到了奇怪的情況,其中j會開始走出數組末尾。

testApp.h的節選

int z, _j=0;
Boolean arp;

編輯2:我現在對此進行了修訂,可以正常工作,很抱歉要求如此基本且具有如此糟糕的代碼。 我很感謝人們在這里發表評論所花費的時間。 這是我修改過的.cpp文件和.h文件供您細讀。 再次感謝。

#include "testApp.h"
#include <iostream>
using namespace std;



testApp::~testApp() {

}

void testApp::setup(){


sampleRate          = 44100;
initialBufferSize   = 1024;

//MidiIn.openPort();
//ofAddListener(MidiIn.newMessageEvent, this, &testApp::newMessage);

j = 0;
z= 0;
state = 1;


tuning = 440;
inputNote = 127;
octave = 4;
sorSpeed = 2;
freqOut = (tuning/32) * pow(2,(inputNote-69)/12);
finalOut = freqOut * octave;



notes[7] = finalOut+640;
notes[6] = finalOut+320;
notes[5] = finalOut+160; 
notes[4] = finalOut+840;
notes[3] = finalOut+160;
notes[2] = finalOut+500;
notes[1] = finalOut+240;
notes[0] = finalOut;


ofSoundStreamSetup(2,0,this, sampleRate, initialBufferSize, 4);/* Call this last ! */


}

void testApp::update(){



}

void testApp::draw(){



}

int testApp::updown(int &_j){

int tmp;

if(_j==0){
    arp = true;
}

else if(_j==7) {
    arp = false; 
}


if(arp == true){
    _j++;
}

else if(arp == false){
    _j--;
}

tmp = _j;
return (tmp);

}

void testApp::audioRequested    (float * output, int bufferSize, int nChannels){


    for (int i = 0; i < bufferSize; i++){


        if ((int)timer.phasor(sorSpeed)) {


            noteOut = notes [updown(z)];


            }


    mymix.stereo(mySine.sinewave(noteOut),outputs,0.5);


    output[i*nChannels    ] = outputs[0]; 
    output[i*nChannels + 1] = outputs[1];

    }
}

testApp.h

class testApp : public ofBaseApp{

public:
    ~testApp();/* destructor is very useful */
    void setup();
    void update();
    void draw();

    void keyPressed  (int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);

    void newMessage(ofxMidiEventArgs &args);

    ofxMidiIn MidiIn;

    void audioRequested     (float * input, int bufferSize, int nChannels); /* output method */
    void audioReceived  (float * input, int bufferSize, int nChannels); /* input method */

    Boolean arp;
    int     initialBufferSize; /* buffer size */
    int     sampleRate;
    int    updown(int &intVar);


    /* stick you maximilian stuff below */

    double filtered,sample,outputs[2];
    maxiFilter filter1;
    ofxMaxiMix mymix;
    ofxMaxiOsc sine1;
    ofxMaxiSample beats,beat;
    ofxMaxiOsc mySine,myOtherSine,timer;

    int currentCount,lastCount,i,j,z,octave,sorSpeed,state;
    double notes[8];
    double noteOut,freqOut,tuning,finalOut,inputNote;

};

將所有這些拼湊起來非常困難。 我確實認為您需要稍微回到基礎知識,但是我認為我可以解釋所有的情況。

  1. 您將_j初始化為0 ,然后再不修改_j的值。
  2. 因此,您每次都調用updown並傳遞0作為參數。
  3. 當輸入為0時, updown返回值1

也許您打算在調用z時將z傳遞給updown ,但我不確定。

您是否真的在頭文件中聲明了全局變量? 這不好。 嘗試盡可能使用局部變量和/或參數。 全局變量非常邪惡,尤其是在頭文件中這樣聲明!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM