操作类中的向量时遇到问题

时间:2013-12-06 19:56:40

标签: c++ class pointers vector reference

我需要定义一个测试模式的向量,然后我可以通过各种方法操作值。

以下是我定义TestMode类的方法:

class TestMode{
public:
    TestMode(int val, int jamramaddr){
        value=val;
        jamramaddress=jamramaddr;
    }
    int getAddr(void){
        return jamramaddress;
    }
    void setValue(int val){
        value=val;
    }
    int getValue(void){
        return value;
    }
private:
    int value;
    int jamramaddress;
};

非常简单。

然后我有一个TestModeGroup类来对我创建的测试模式向量执行操作。该课程如下:

class TestModeGroup{
public:
    TestModeGroup(const std::vector<TestMode> &TestModes){
        TestModeVector=TestModes;
    }
    //Compare the given jamramaddress against the known jamramaddress of the given testmode. If it is a match then update the testmode value
    void compareAndStore(TestMode &TM){
        int TM_address=TM.getAddr();
        if(TM_address==JamRamAddress){
            output("Match found! Old TM value %d", TM.getValue());
                TM.setValue(JamRamData);
            output("New TM value %d", TM.getValue());
        }
    }
    //Commit the given testmode to the jamram with the latest known value
    void writeTmBitToJamRam(TestMode &TM){
        JamRamAddress=TM.getAddr();
        JamRamData=TM.getValue();
        apg_jam_ram_set(JamRamAddress,JamRamData);
    }
    //running TestModeGroupObject.store(address, data) will find which test mode that jamram address is for and set the appropriate test mode value for later printing.
    //This is meant to be used in conjuction with the Excel spreadsheet method of entering test modes
    void store(int address, int data){
            JamRamAddress= address;
            JamRamData   = data;
            output("Current JamRamAddress = %d JamRamData = %d", JamRamAddress, JamRamData);
            apg_jam_ram_set(JamRamAddress,JamRamData);
            for(std::vector<TestMode>::iterator it = TestModeVector.begin(); it!=TestModeVector.end(); ++it){
                compareAndStore(*it);
            }
    }
    //Running TestModeGroupObject.load() will commit all test mode changes to the jamram for test modes that are part of that object
    void load(void){
            for(std::vector<TestMode>::iterator it = TestModeVector.begin(); it!=TestModeVector.end(); ++it){
                writeTmBitToJamRam(*it);
            }
    }
    int getTMVal(TestMode &TM){
        return TM.getValue();
    }
private:
    int JamRamAddress;
    int JamRamData;
    std::vector<TestMode> TestModeVector;
};

以下是我定义矢量的方法:

TestMode adm_testmodes[] = {TM_TWINWL,TM_TWINBL,ON_2WL,ON_2BL,WV_S1X,WV_S0X,TM_PCHG_RH_3,TM_PCHG_RH_2,TM_PCHG_RH_1,TM_PCHG_RH_0,TM_PCHG_RH_BYP,TM_PCHG_SF_3,TM_PCHG_SF_2,TM_PCHG_SF_1,TM_PCHG_SF_0,TM_PCHG_SF_BYP,
    TM_PCHG_V04_3,TM_PCHG_V04_2,TM_PCHG_V04_1,TM_PCHG_V04_0,TM_PCHG_V04_BYP,TM_SA_DIS,TM_TS_NEGSLOPE,TM_TRIM_4,TM_TRIM_3,TM_TRIM_2,TM_TRIM_1,TM_TRIM_0,TM_TSSLP_2,TM_TSSLP_1,TM_TSSLP_0,
    TM_WRV_N_2,TM_WRV_N_1,TM_WRV_N_0,TM_SAGAIN_EN,TM_REFTRIM_EN,TM_READ_DONE_OPT_EN,EnableCore_Read,SA_4,TM_OC_2,TM_OC_1,TM_OC_0,TM_WRLC_4,TM_WRLC_3,TM_WRLC_2,TM_WRLC_1,TM_WRLC_0,
    TM_WRHC_4,TM_WRHC_3,TM_WRHC_2,TM_WRHC_1,TM_WRHC_0,TM_FTOP_3,TM_FTOP_2,TM_FTOP_1,TM_FTOP_0,TM_RISE_1,TM_RISE_0,TM_WRH_3,TM_WRH_2,TM_WRH_1,TM_WRH_0,TM_SET_4,TM_SET_3,TM_SET_2,TM_SET_1,TM_SET_0,
    TM_REFSTART,TM_REFSEL_1,TM_REFSEL_0,TM_REFSL_5,TM_REFSL_4,TM_REFSL_3,TM_REFSL_2,TM_REFSL_1,TM_REFSL_0,TM_REFSH_5,TM_REFSH_4,TM_REFSH_3,TM_REFSH_2,TM_REFSH_1,TM_REFSH_0,TM_READ_DONE_ADD,
    TM_READ_DONE_OPT,TM_READ_DONE_5,TM_READ_DONE_4,TM_READ_DONE_3,TM_SAGAIN_1,TM_SAGAIN_0,TM_REFR_5,TM_REFR_4,TM_REFR_3,TM_REFR_2,TM_REFR_1,TM_REFR_0   
};

std::vector<TestMode> ADM_TMs (adm_testmodes, adm_testmodes + sizeof(adm_testmodes) / sizeof(TestMode));

TestModeGroup ADM_TestModeGroup(ADM_TMs);

到目前为止一切顺利。我可以直接访问已定义的所有TestMode,更改值并使更改在任何地方持续存在。当我尝试在TestModeGroup类中运行“store”函数时出现问题。似乎我有一个更新的TestModes的本地副本,但不是原始的TestMode。我确信这是一个非常简单的问题,但我很挣扎。 C ++不是我的专长,而OOP更是如此。

以下是我创建的虚拟测试模式的快速示例:

output("DUMMY_TESTMODE Initial Value: %d", DUMMY_ADM_TestModeGroup.getTMVal(DUMMY_TESTMODE));
DUMMY_TESTMODE.setValue(1);
output("DUMMY_TESTMODE Set to 1 Value: %d", DUMMY_ADM_TestModeGroup.getTMVal(DUMMY_TESTMODE));
DUMMY_TESTMODE.setValue(0);
output("DUMMY_TESTMODE Set to 0 Value: %d", DUMMY_ADM_TestModeGroup.getTMVal(DUMMY_TESTMODE));
DUMMY_ADM_TestModeGroup.store(22,1);
output("DUMMY_TESTMODE Set to 1 Value: %d", DUMMY_ADM_TestModeGroup.getTMVal(DUMMY_TESTMODE));

执行.setValue工作正常,但做.store却没有。当我打印出它返回的值为0.在.store函数中我打印输出,但是它给出了正确的值1.不知怎的,我认为我只是在改变原始矢量的副本,但我只是无法弄清楚。我一直在疯狂,没有人跟我谈过,对C ++有足够的了解。有没有人知道我搞砸了哪里?

1 个答案:

答案 0 :(得分:1)

尝试更改TestModeGroup类:

class TestModeGroup{
public:
    TestModeGroup(const std::vector<TestMode> &TestModes) : TestModeVector(TestModes)
    {}

    // ...

    std::vector<TestMode> & TestModeVector;
};

如果要将类内部完成的向量修改应用于传递给构造函数的原始值,则需要存储对原始对象的引用。如果这有帮助,请告诉我:)。