C++ 在类中操作向量时遇到问题
我需要定义一个testmodes向量,然后通过各种方法来操作的值 下面是我如何定义TestMode类的:C++ 在类中操作向量时遇到问题,c++,class,pointers,vector,reference,C++,Class,Pointers,Vector,Reference,我需要定义一个testmodes向量,然后通过各种方法来操作的值 下面是我如何定义TestMode类的: class TestMode{ public: TestMode(int val, int jamramaddr){ value=val; jamramaddress=jamramaddr; } int getAddr(void){ return jamramaddress; } void setValu
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类来对我创建的testmodes向量执行操作。该类如下所示:
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;
};
执行.setValue可以正常工作,但执行.store则不行。当我打印出值时,它返回为0。但是,在.store函数中,我进行了打印输出,得到了正确的值1。不知何故,我认为我只是在改变原始向量的一个副本,但我就是搞不懂。我一直在发疯,没人跟我交流过,知道C++有帮助。有人知道我把事情搞砸了吗?尝试更改TestModeGroup类:
class TestModeGroup{
public:
TestModeGroup(const std::vector<TestMode> &TestModes) : TestModeVector(TestModes)
{}
// ...
std::vector<TestMode> & TestModeVector;
};
类TestModeGroup{
公众:
TestModeGroup(const std::vector和TestModes):TestModeVector(TestModes)
{}
// ...
std::vector和TestModeVector;
};
如果希望在类内完成的向量修改应用于传递给构造函数的原始值,则需要存储对原始对象的引用。如果有帮助,请告诉我:)尝试更改TestModeGroup类:
class TestModeGroup{
public:
TestModeGroup(const std::vector<TestMode> &TestModes) : TestModeVector(TestModes)
{}
// ...
std::vector<TestMode> & TestModeVector;
};
类TestModeGroup{
公众:
TestModeGroup(const std::vector和TestModes):TestModeVector(TestModes)
{}
// ...
std::vector和TestModeVector;
};
如果希望在类内完成的向量修改应用于传递给构造函数的原始值,则需要存储对原始对象的引用。让我知道这是否有用:)如果我做了更改,我甚至无法编译。“'TestModeVector':必须在构造函数基/成员初始值设定项列表中初始化”您必须在构造函数中使用初始值设定项列表:
TestModeGroup(const std::vector&TestModes):TestModeVector(TestModes){}
如果进行了更改,我甚至无法编译。“'TestModeVector':必须在构造函数基/成员初始值设定项列表中初始化”您必须在构造函数中使用初始值设定项列表:TestModeGroup(const std::vector&TestModes):TestModeVector(TestModes){}
class TestModeGroup{
public:
TestModeGroup(const std::vector<TestMode> &TestModes) : TestModeVector(TestModes)
{}
// ...
std::vector<TestMode> & TestModeVector;
};