[英]getting direct access to data stored in vector from one class to another
我有一个名为table的类,它具有一个作为公共成员的向量,该表类调用一种将数据添加到向量中的方法。 现在我想从表类到NextClass访问向量中存储的这些数据。 我尝试使用继承,例如NextClass:table可以正常工作,但实际上NextClass不是表类,它是“与Table类有关系”,我还尝试使表类的类实例成为NextClass,但是这样做将不允许我访问向量中的数据,因为它会返回一个空向量。 请查看我要实现的目标的抽象代码
表类:
class Table
{
public:
Table();
vector<vector<float> > vec; //innitializing vector
void addTableData(); //method to add data in vector
}
现在我有另一个课NextClass
class NextClass
public:
NextClass();
vector<vector<float> > nextVector; //new vector to store value from above class
vector<float> innervec;
void copyVectorToThisClass(); // method to copy vector from table class to this class
private:
Table table; //also tried with making pointer Table *table but no luck
将向量数据从表复制到Next类的方法
void NextClass::copyVectorToThisClass(){
for( int i = 0; i<vec.size();i++)
for (unsigned int ii=0; ii<table.vec.size();ii++)
{
innervec.assign(table.vec[i].begin()+ii,table.vec[i].end());
//also tried with vec.pushback
}
nextVector.push_back(innervec);
}
编译时没有错误,但是当我尝试检查newVector是否保存任何数据时,它返回空值,这意味着新Vector中没有数据传输,如果有一种方法可以操纵指针指向类表中的数据那么完成这段代码将是很棒的..
编辑代码
表类:
class Table
{
public:
Table();
void addTableData(); //method to add data in vector
private:
vector<vector<float> > vec; //innitializing vector
}
现在我有另一个课NextClass
class NextClass
public:
NextClass();
vector<vector<float> > nextVector; //new vector to store value from above class
vector<float> innervec;
void copyVectorToThisClass(); // method to copy vector from table class to this class
Table table; //also tried with making pointer Table *table but no luck
A method to copy vector data from table to Next class
void NextClass::copyVectorToThisClass(){
for( int i = 0; i<vec.size();i++)
for (unsigned int ii=0; ii<table.vec.size();ii++)
{
innervec.assign(table.vec[i].begin()+ii,table.vec[i].end());
//also tried with vec.pushback
}
nextVector.push_back(innervec);
}
非常感谢。
您可以在函数的开头初始化向量,然后对for循环vec.size()进行操作,使其为0,因为向量中没有任何内容。
您似乎有一些概念上的错误:
我建议您采用以下结构:
将copyVector更改为table,因为由于vec是table的一部分,因此由谁来决定是否复制其数据。
class table
{
....
void getVector( const vector<float>& next ) const
}
void table::getVector( vector<float>& next ) const
{
next = vec; //default = operator
}
然后,您可以像这样在NextClass中使用它:
void NextClass::copyVectorToThisClass( const table& t)
{
t.getVector( nextVector );
}
(好吧,代码是“即时”制作的,也许有错字)
struct Table {
vector<vector<float> > vec;
vector<vector<float> > & getDataRef() { return vec; }
};
class NextClass {
vector<vector<float> > nextClassVec;
public NextClass(Table t) {
nextClassVec = t.getDataRef(); // method #1, maybe?
}
public readTable (const struct Table& tbl) { // method 2 ..?
const vector<vector<float>> &tblVecRow = tbl.vec;
for(int i = 0; i < tblVecRow.size();++i) {
const vector<float> & tblVecData = tblVecRow[i];
vector<float> nextClassVecData;
for(int j = 0; j < tblVecData.size(); ++j) {
nextClassVecData.push_back(tblVecData[j]);
}
nextClassVec.push_back(nextClassVecData);
}
}
};
这将对向量数据进行深层复制(至少这是个主意-我尚未对此进行编译)。 可能有比这更好的解决方案(例如使用copy()算法); 如果是这样,欢迎大家进行改进。
其他说明:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.