How to convert vector with datatype double to a vector with certain class datatype?

Discussion in 'C' started by Roy420, Aug 12, 2008.

  1. Roy420

    Roy420 New Member

    Joined:
    Aug 12, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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

    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 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
    :D
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You should use code block rather than Italics.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What you'll need to do is to create an array of doubles using double *arr=new double[Vect_XX_AT.size()] (now put a delete[] somewhere in such a way as it will cause a compiler error - forcing yourself to deal with it before proceeding), populate it from Vect_XX_AT, then you can use the NRVec(const T *a,int n) constructor.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice