Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CArray problem

CArray problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdata-structures
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trupti Mehta
    wrote on last edited by
    #1

    Working with EVc++4 on ARMV4I device. I have an CArray :

    CArray<CStockEntry, CStockEntry> stockArr;
    void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr);
    CArray<CStockEntry, CStockEntry>& GetStockEntryArr();

    in my .cpp:

    void DeptStock::SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) {
    for (int i=0; i <= arr.GetUpperBound(); i++) {
    CStockEntry se = arr.GetAt(i);
    this->stockArr.Add(se);
    }

    CString s1 = _T("Size of copied Array & Org:");
    s1.Format(_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize()); // This shows same number of arr & stockArr GetSize
    AfxMessageBox(s1);

    s1.Empty();
    return;
    }

    CArray<CStockEntry, CStockEntry>& DeptStock::GetStockEntryArr() {
    CString s1 = _T("Size of Returning Array:");
    s1.Format(_T("%s %d"), s1, stockArr.GetSize()); // This shows 0 as GetSize
    AfxMessageBox(s1);

    s1.Empty();

    return this->stockArr;
    }

    In between Set & Get I don't perform any operations on the stockArr. Why is while set, it is stored properly, but while retriing I get an empty array. Any help is appreciated.

    Thanks Terry

    M 1 Reply Last reply
    0
    • T Trupti Mehta

      Working with EVc++4 on ARMV4I device. I have an CArray :

      CArray<CStockEntry, CStockEntry> stockArr;
      void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr);
      CArray<CStockEntry, CStockEntry>& GetStockEntryArr();

      in my .cpp:

      void DeptStock::SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) {
      for (int i=0; i <= arr.GetUpperBound(); i++) {
      CStockEntry se = arr.GetAt(i);
      this->stockArr.Add(se);
      }

      CString s1 = _T("Size of copied Array & Org:");
      s1.Format(_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize()); // This shows same number of arr & stockArr GetSize
      AfxMessageBox(s1);

      s1.Empty();
      return;
      }

      CArray<CStockEntry, CStockEntry>& DeptStock::GetStockEntryArr() {
      CString s1 = _T("Size of Returning Array:");
      s1.Format(_T("%s %d"), s1, stockArr.GetSize()); // This shows 0 as GetSize
      AfxMessageBox(s1);

      s1.Empty();

      return this->stockArr;
      }

      In between Set & Get I don't perform any operations on the stockArr. Why is while set, it is stored properly, but while retriing I get an empty array. Any help is appreciated.

      Thanks Terry

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I tested your code as follows:

      struct CStockEntry
      {
      int entry;
      CStockEntry(int i = 0) {entry = i;}
      };

      class DeptStock
      {
      public:
      CArray<CStockEntry, CStockEntry> stockArr;

      void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) 
      {   
      	for (int i=0; i <= arr.GetUpperBound(); i++) 
      	{   
      		CStockEntry se = arr.GetAt(i);   
      		this->stockArr.Add(se);   
      	}   
      
      	CString s1 = \_T("Size of copied Array & Org:");   
      	s1.Format(\_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize());  // This shows same number of arr & stockArr GetSize 
      	AfxMessageBox(s1);   
      
      	s1.Empty();   
      	return;   
      }   
      
      CArray<CStockEntry, CStockEntry>& GetStockEntryArr() 
      {   
      	CString s1 = \_T("Size of Returning Array:");   
      	s1.Format(\_T("%s %d"), s1, stockArr.GetSize());  // This shows 0 as GetSize 
      	AfxMessageBox(s1);   
      
      	s1.Empty();   
      
      	return this->stockArr;   
      }   
      

      };

      ...

      CArray<CStockEntry, CStockEntry> stockArr;
      stockArr.Add(CStockEntry(0));
      stockArr.Add(CStockEntry(1));
      stockArr.Add(CStockEntry(2));
      stockArr.Add(CStockEntry(3));
      stockArr.Add(CStockEntry(4));
      DeptStock test;
      test.SetCStockEntryArr(stockArr);
      CArray<CStockEntry, CStockEntry> &stockArrRef = test.GetStockEntryArr();
      

      It works fine. What aren't you showing? FWIW, calling Empty() on those local CStrings is unnecessary. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      T 1 Reply Last reply
      0
      • M Mark Salsbery

        I tested your code as follows:

        struct CStockEntry
        {
        int entry;
        CStockEntry(int i = 0) {entry = i;}
        };

        class DeptStock
        {
        public:
        CArray<CStockEntry, CStockEntry> stockArr;

        void SetCStockEntryArr(CArray<CStockEntry, CStockEntry>& arr) 
        {   
        	for (int i=0; i <= arr.GetUpperBound(); i++) 
        	{   
        		CStockEntry se = arr.GetAt(i);   
        		this->stockArr.Add(se);   
        	}   
        
        	CString s1 = \_T("Size of copied Array & Org:");   
        	s1.Format(\_T("%s %d %d"), s1, stockArr.GetSize(), arr.GetSize());  // This shows same number of arr & stockArr GetSize 
        	AfxMessageBox(s1);   
        
        	s1.Empty();   
        	return;   
        }   
        
        CArray<CStockEntry, CStockEntry>& GetStockEntryArr() 
        {   
        	CString s1 = \_T("Size of Returning Array:");   
        	s1.Format(\_T("%s %d"), s1, stockArr.GetSize());  // This shows 0 as GetSize 
        	AfxMessageBox(s1);   
        
        	s1.Empty();   
        
        	return this->stockArr;   
        }   
        

        };

        ...

        CArray<CStockEntry, CStockEntry> stockArr;
        stockArr.Add(CStockEntry(0));
        stockArr.Add(CStockEntry(1));
        stockArr.Add(CStockEntry(2));
        stockArr.Add(CStockEntry(3));
        stockArr.Add(CStockEntry(4));
        DeptStock test;
        test.SetCStockEntryArr(stockArr);
        CArray<CStockEntry, CStockEntry> &stockArrRef = test.GetStockEntryArr();
        

        It works fine. What aren't you showing? FWIW, calling Empty() on those local CStrings is unnecessary. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        T Offline
        T Offline
        Trupti Mehta
        wrote on last edited by
        #3

        Hi Mark, If I add objects in array like you are adding, then my code is working perfect. But I am working different way. In my GUI, I add objects in the array in my GUI CPage4 (m_page4 member var), then on Ok button I create DeptStock deptStock object & SetCStockEntryArr(m_page4.GetStockEntries()). TILL this things are fine. Then I pass deptStock to a file to store the object details in file. g_dbOperations.Write_StockTextFile(deptStock);

        // WRITE Departments MAP TO FILE
        void DbOperations::Write_StockTextFile(DeptStock deptStock) {

            .........  
        CString toWrite;
        int index = -1;
        
        OperatorDetails od = deptStock.GetOperator();
        DeptDetails dd = deptStock.GetDept();
        CArray<cstockentry,>& stockArr = deptStock.GetStockEntryArr();  // This returns 0 sizxe array
        

        // stockArr = deptStock.GetStockEntryArr();

        /* CStockEntry cse1(2.2f, 5, 10.2f);
        CStockEntry cse2(5.0f, 3, 15.0f);
        stockArr.Add(cse1);
        stockArr.Add(cse2);
        */
        CString str(_T("Stock Size:"));
        str.Format(_T("%s %d \n %s %d"), str, stockArr.GetSize(), _T("Dept Stock Arr "), deptStock.GetStockEntryArr().GetSize());
        AfxMessageBox(str); // Both DISPLAYS 0

        This is how the flow is. If I remove the above comments of adding objects od CStockEntry to stockArr, it gives proper results. But I have already added the array elements & not able to retrive. I have given a better & clear picture of the flow of my program & error point. Hope you can help me out to track the problem.

        Thanks Terry

        M 1 Reply Last reply
        0
        • T Trupti Mehta

          Hi Mark, If I add objects in array like you are adding, then my code is working perfect. But I am working different way. In my GUI, I add objects in the array in my GUI CPage4 (m_page4 member var), then on Ok button I create DeptStock deptStock object & SetCStockEntryArr(m_page4.GetStockEntries()). TILL this things are fine. Then I pass deptStock to a file to store the object details in file. g_dbOperations.Write_StockTextFile(deptStock);

          // WRITE Departments MAP TO FILE
          void DbOperations::Write_StockTextFile(DeptStock deptStock) {

              .........  
          CString toWrite;
          int index = -1;
          
          OperatorDetails od = deptStock.GetOperator();
          DeptDetails dd = deptStock.GetDept();
          CArray<cstockentry,>& stockArr = deptStock.GetStockEntryArr();  // This returns 0 sizxe array
          

          // stockArr = deptStock.GetStockEntryArr();

          /* CStockEntry cse1(2.2f, 5, 10.2f);
          CStockEntry cse2(5.0f, 3, 15.0f);
          stockArr.Add(cse1);
          stockArr.Add(cse2);
          */
          CString str(_T("Stock Size:"));
          str.Format(_T("%s %d \n %s %d"), str, stockArr.GetSize(), _T("Dept Stock Arr "), deptStock.GetStockEntryArr().GetSize());
          AfxMessageBox(str); // Both DISPLAYS 0

          This is how the flow is. If I remove the above comments of adding objects od CStockEntry to stockArr, it gives proper results. But I have already added the array elements & not able to retrive. I have given a better & clear picture of the flow of my program & error point. Hope you can help me out to track the problem.

          Thanks Terry

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Trupti Mehta wrote:

          If I add objects in array like you are adding, then my code is working perfect.

          That should help you track it down. If your array is empty, debug the code that's supposed to populate it...why are no items added? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups