0

質問してから 8 時間以内に自分の質問に答えることができないため、ここに解決策を投稿します。

入力チャネル番号とベクトル要素の番号に誤りがありました。channel の代わりに channel-1 の値を設定して問題を修正しました。

私の新しい機能は次のとおりです。

void input(long inlet, t_symbol *s, long ac, t_atom *av){
    // GET VARIABLES
    long channel = atom_getlong(av);
    double value = atom_getfloat(av + 1);
    long v_size = v_chan.size();
    if(channel && v_size < channel){
        for(int i = v_size; i < channel; i++){
            v_chan.push_back(n_chan);
        }
        v_chan[channel - 1].value = value;
    }
    else if(channel){
        v_chan[channel - 1].value = value;
    }
}

新しい空の構造体で push_back するのが好きな、構造体を含むベクターがあります。
コード例:

struct channels{
  double value;
  // eventually more variables
};

vector<channels> v_chan;
channels n_chan;

void push(){
  v_chan.push_back(n_chan);
}

問題は、私のベクトルに要素が含まれている場合、push_back が要素を追加するだけでなく、最後の要素を上書きすることです。

たとえば、ベクトル サイズが 1 で要素 0 の値が 0.2 の場合、push_back 後、ベクトル サイズは 2 ですが、要素 0 と 1 の値は 0 になります。

ここで何が間違っていますか?

Real Code: (MAX/MSP 外部、関数入力は Max で呼び出されます)

#include <maxcpp6.h>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

struct bind{
    string param;
    double* value;
    int track;
    double base;
    double multiplier;
};

struct channels{
    double value;
    vector<int> bind;
};

vector<channels> v_chan;
vector<bind> v_bind(19);
channels n_chan;

class rec : public MaxCpp6<rec> {
public:
rec(t_symbol * sym, long ac, t_atom * av) {
    setupIO(1, 1); // inlets / outlets
}
~rec() {}

// methods:


//SET BIND FUNCTION
void setBind(long inlet, t_symbol *s, long ac, t_atom *av){
}

void output(long track, long type){

}

void input(long inlet, t_symbol *s, long ac, t_atom *av){
    // GET VARIABLES
    long channel = atom_getlong(av);
    double value = atom_getfloat(av + 1);
    long v_size = v_chan.size();
    if(v_size <= channel){
        v_chan.push_back(n_chan);
    }
    else{
        v_chan[channel].value = value;
    }
}

void dump(long inlet){
    for(int i = 1; i <= v_chan.size(); i++){
        post("%d %.2f", i, v_chan[i].value);
    }
}

    void clearTrackBinds(long inlet){

    }

    void reset(long inlet){
        clearTrackBinds(0);
    }
};

C74_EXPORT int main(void) {
    // create a class with the given name:
    rec::makeMaxClass("solar_receiver");
    REGISTER_METHOD_GIMME(rec, input);
    REGISTER_METHOD_GIMME(rec, setBind);
    REGISTER_METHOD(rec, dump);
    REGISTER_METHOD(rec, clearTrackBinds);
    REGISTER_METHOD(rec, reset);
}
4

0 に答える 0