The issue I am encountered with is… the same vector we read as double is to be used in another function, but with different data type as mentioned bellow. So I need a conversion function which performs this fact.

I have a class as follows

[i]template <class T>
class NRVec {
private:
int nn; // size of array. upper index is nn-1
T *v;
public:
NRVec();
explicit NRVec(int n); // Zero-based array
NRVec(const T &a, int n); //initialize to constant value
NRVec(const T *a, int n); // Initialize to array
inline const T & operator[](const int i) const;
inline int size() const;
~NRVec();
};[/i]

I have a vector defined as follows

typedef const NRVec<DP> Vec_I_DP;
I have a another class having a function svdfit() and this function takes a argument (vector) with datatype from the class NRVec.

void AutoCompensation::svdfit(Vec_I_DP &x, Vec_I_DP &y)

The problem arises when I have my own vectors with type

typedef double DP;
vector <DP> v,Vect_XX_AT, Vect_YY_AT;

Now instead of using Vec_I_DP, I want to use my vector Vec_XX_AT. The problem is difference in the datatype. So I wanted to have a conversion function which converts vector from datatype Vec_I_DP(Vec_I_DP &x) to DP(vector <DP> Vect_XX_AT) as shown above.

How to do it??

Merci

Edward doesn't see a problem converting between NRVec<double> and vector<double>, but because NRVec isn't designed to integrate with the STL, it's slightly harder than it should be. Converting a vector<double> to NRVec<double> can be done with the NRVec<> constructor like this:

std::vector<double> a;

...

NRVec<double> b(&a[0], a.size());

Using the address of the first element of a vector is the same as getting a pointer to the first element of an array, and that conveniently matches one of NRVec<>'s constructors.

Converting an NRVec<double> to vector<double> is harder because you need to write a manual loop instead of just using vector's constructor that takes a range:

NRVec<double> a(N);

...

std::vector<double> b(a.size());

for (int i = 0; i < a.size(); ++i)
  b[i] = a[i];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.