Reputation: 166
I have a class called table, It has a vector as public member, this table class calls a method which adds data into the vector. Now i want to access these data stored in vector from table class to NextClass. I tried to use inheritance, for instance NextClass:table which works fine but in reality NextClass is not a table class it is an "has a relation to Table class", I also tried to make class instance of the table class into NextClass, but doing so will not allow me to access data in the vector, as a resutl it returns an empty vector. Please see an abstract code of what I am trying to achieve
table class:
class Table
{
public:
Table();
vector<vector<float> > vec; //innitializing vector
void addTableData(); //method to add data in vector
}
Now I have another class 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
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);
}
There is no error while compiling, but when I tried to check if the newVector holds any data then it returns empty that means no data transfer in the new Vector, If there is a way to manipulate pointer to point at the data inside the class table then would be great to finish this piece of code..
Edited code
table class:
class Table
{
public:
Table();
void addTableData(); //method to add data in vector
private:
vector<vector<float> > vec; //innitializing vector
}
Now I have another class 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);
}
With much appreciation.
Upvotes: 1
Views: 674
Reputation: 14855
You seem to have some conceptual errors:
I suggest you a following structure:
change the copyVector to table, because as vec is part of table, it is who have the final decision about copying or not his data.
class table
{
....
void getVector( const vector<float>& next ) const
}
void table::getVector( vector<float>& next ) const
{
next = vec; //default = operator
}
And then, you may use it inside NextClass like that:
void NextClass::copyVectorToThisClass( const table& t)
{
t.getVector( nextVector );
}
(Well, the code is made "on the fly", there is maybe a typo)
Upvotes: 1
Reputation: 1
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);
}
}
};
This does a deep copy of the vector data (at least that's the idea - I haven't compiled this). There is [very] likely to be much better solutions than this (like using the copy() algorithm); if so the folks are welcome to improve it.
Other notes:
Upvotes: 0
Reputation: 15824
Upvotes: 0
Reputation: 51155
You initialize your vector at the beginning of the function, and then run the for loop to vec.size() which is 0, because there is nothing in the vector.
Upvotes: 1