This seems to 开发者_开发问答very very complicated, I tried a lot to solve it, but when I resize a private member inside the template/class, i.e., when I call Operator::InitExtVector, I resize intVect through its address, and when I show/call Operator::ShowExtVector its size is 0 again, which should not be happening. Why the vector information is lost?
I have a template/class, Base.H:
#include <vector>
typedef std::vector <unsigned long int > varVect1D_ul;
<template <typename PS>
class Base{
private:
PS bla;
varVect1D_ul intVect;
public:
varVect1D_ul* dataVect(){return &intVect}
}
some operations are done within the Operator class, normal one no template,Operator.H:
class Operator{
public:
.....
private:
void InitExtVector(std::vector <Base<float> > BaseVect){
varVect1D_ul *pVect;
for(i=0...BaseVect.size()){
pVect=dataVect();
pVect->resize(10);
}
} // end InitExtVector
void ShowExtVector(std::vector <Base<float> > BaseVect){
varVect1D_ul *pVect;
for(i=0...BaseVect.size()){
pVect=BaseVect[i].dataVect();
cout<<pVect->size();
}
}//end ShowExtVector
}; //end class
, and client code looks like someOperations.C:
#include "Base.H"
#include "Operator.H"
foo(){
std::vector <Base<float> > BaseVect(2);
//do some initization of BseVect;
Operator *ObjOper= new Operator;
ObjOper->InitExtVector(BaseVect);
ObjOper-> ShowExtVector(BaseVect);
}
All syntax is Ok , the addresses of pVect inside InitExtVect and ShowExtVector are the same
Any help will be gladly appreciated
Regard
JORR
I think you want to take your vectors in by reference:
void InitExtVector(std::vector <Base<float> > & BaseVect);
void ShowExtVector(std::vector <Base<float> > & BaseVect);
Also, did you mean this in InitExtVector:
for(i=0 ; i < BaseVect.size() ; ++i){
pVect=BaseVect[i].dataVect();
pVect->resize(10);
}
精彩评论