Link to home
Start Free TrialLog in
Avatar of Carl3003
Carl3003

asked on

probably a bug in MS VSC++ 6.0???

I have this simple program , i dont know why but i get same refference memory error" The instruction at '0x004022ce' referenced memory at '0xddddddddd'. The memory could not read".. Here is the sample. Can anyone help me to solve that? Thanks
#include <list>
#include <iostream>
#include <string>

using namespace std ;

class Apple;
class Basket;
typedef list<Apple> LISTApple;


class Apple
{
public:
      Apple(){second=0;first=0;};
      void setDATA(int, int);
    int getfirst();
      int getsecond();

private:
      int first;
      int second;

};
class Basket
{
public:
      void addNewApple(Apple);
      Apple getFirstApple();
      void showList();
private:

            LISTApple listApple;
};


void main()
{
      Apple newApple;
      Basket myReadyQ;

      newApple.setDATA(6, 3);
      myReadyQ.addNewApple(newApple);
      cout<<"second = "<<myReadyQ.getFirstApple().getsecond()<<endl;
      cout<<"first= "<<myReadyQ.getFirstApple().getfirst()<<endl;  // it works fine without to cout this statement
}

void Apple::setDATA(int First, int Second)
{
      first=First;
      second=Second;

}

int Apple::getfirst()
{
      return first;
}

int Apple::getsecond()
{
      return second;
}

void Basket::addNewApple(Apple newApple)
{

      listApple.push_front(newApple);            

}


Apple Basket::getFirstApple()
{
      LISTApple::iterator i;
      Apple selectedApple;

      i= listApple.begin();
      selectedApple=(*i);

      listApple.pop_back();
      return selectedApple;
}

void Basket::showList()
{
      LISTApple::iterator i;


      for (i = listApple.begin(); i != listApple.end(); ++i)
                cout <<"First = "<<(*i).getfirst()<<", second = "<<(*i).getsecond()<<endl;
}
Avatar of Sys_Prog
Sys_Prog
Flag of India image

The  problem is with this line

 listApple.pop_back();
in getFirstApple()

When u first call
cout<<"second = "<<myReadyQ.getFirstApple().getsecond()<<endl;

It executes the code in getFirstApple() and thus removes the only elemnet that is present in your list
Then again when u call
cout<<"first= "<<myReadyQ.getFirstApple().getfirst()<<endl;

It again tries to access the element (apple) which is not present

Amit
You should either NOT remove the elemnt from from list i.e
listApple.pop_back(); should not be there in your getFirstApple() function since u need to reference the list element again

OR
u should check for the size of list and if it is no zero, then reference the iterator
somethinglike
if ( listApple.size() != 0 ) {
            i= listApple.begin();
        selectedApple=(*i);
        listApple.pop_back();
}

OR
check for listApple.begin() != listApple..end() in the above if condition

Amit
Avatar of Carl3003
Carl3003

ASKER

getfirst() and getsecond() is supposed to return the element  not to remove it
oh i got you....
ASKER CERTIFIED SOLUTION
Avatar of Sys_Prog
Sys_Prog
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, i will try to do it right now..
works, thanks a lot