0

I have declared following structure to save data retrieved from access db by getfieldvalue: the error was at Spds_old->Time_old.push_back(OldTime); In the DB the value is 3:00:00 PM. Can you please let me know the error. Thank You. Please let me know if you need more info. Thank You -Kiran

typedef struct {
        std::vector<CString> pname_old;
        std::vector<CDBVariant> Date_old;
        std::vector<CDBVariant> Time_old;

    } Spd_old;

Spd_old *Spds_old;

Then in the cpp file this is the code

                CRecordset rset(&pdatabase);
                CString pName;
                CDBVariant OldDate;
                CDBVariant  OldTime;



CString selectionStr = "SELECT I.PType,I.Date,I.Time FROM CCYX I,(select PType, MAX(Date) AS Date1";
                selectionStr += " FROM CCYX GROUP BY PType) T WHERE I.PType = T.PType AND I.Date =T.Date1"; 




                try
                {
                if(rset.Open(CRecordset::forwardOnly, selectionStr))
                {

                    try
                    {
                        while(!(rset.IsBOF()&&rset.IsEOF()))
                        {
                            m_Log->Log("Copying Previous spd Values from Access DB");
                            rset.GetFieldValue((short)0 ,pName );
                            rset.GetFieldValue((short)1 ,OldDate );
                            rset.GetFieldValue((short)2 ,OldTime);

                            Spds_old->pname_old.push_back(pName);
                            Spds_old->Date_old.push_back(OldDate);
                            Spds_old->Time_old.push_back(OldTime);
user1783998
  • 167
  • 1
  • 1
  • 9

1 Answers1

3

The problem is CDBVariant cannot be copied, so it is not fit for being stored in a std::vector. One workaround is to store pointers instead:

std::vector<std::unique_ptr<CDBVariant>> Date_old;
std::vector<std::unique_ptr<CDBVariant>> Time_old;
...
std::unique_ptr<CDBVariant> OldDate(new CDBVariant);
...
rset.GetFieldValue((short)1 , *OldDate);
...
Spds_old->Date_old.push_back(std::move(OldDate));

I used std::unique_ptr in the example above, but you could use other options also.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • Thank You. It's working. What other pointers can i use? There is an error, can't access private members. May be i need to move them to global. I'll try to fix it. Thank You – user1783998 Dec 26 '12 at 23:48
  • @user1783998: Please see [Which kind of pointer do I use when?](http://stackoverflow.com/questions/8706192/which-kind-of-pointer-do-i-use-when) for information on what type of pointer to use. As for the error, I cannot see all the code, so you would have to provide more information about that. – Jesse Good Dec 27 '12 at 01:56
  • Thank You for the link, i'll go through it and get back to you. Thank You again. – user1783998 Dec 27 '12 at 02:24