vendredi 29 mai 2020

Changes made to member of object, inside object, inside a vector, do not survive end of method. C++

Sorry for the title. I cannot made something more descriptive or clear than that..

I'm trying to do something that resembles a very basic auction.

To simplify things, I'm going to put here the essentials of every class. I'm also translated everything from spanish to english, so if there is a typo, it's only here. The actual code compile fine.

There are four classes involved here:

class Person
{
private:
    std::string name;
}

class Offer
{
private:
    int amount;
    Person *bidder;
}

class Item
{
private:
    int itemNumber;
    std::string itemName;
    Offer *biggestBidder;

public:
    Item(int, std::string)
    void setbiggestBidder(Offer* const &); // I did this '* const &' thing while trying solutions
    Oferta *getBiggestBidder();
}

class Auction
{
private:
    std::vector<Item> itemCollection;
    int amountOfItems; // in above's collection 
}

I didn't put the getters, setters, and constructors/destructors here so it's not too long. Inside Item.cpp I have:

Item::Item(int number, std::string name)
:itemNumber(number), 
itemName(name), 
biggestBidder(NULL)
{
    // this->itemNumber= number;
    // this->itemName= name;
    // this->biggestBidder= NULL;
}

void Item::setbiggestBidder(Offer* const &offer)
{
    if (biggestBidder== NULL || biggestBidder->getAmount() < offer->getAmount())
    {
        std::cout << "Address of offer in setbiggestBidder: " << offer << std::endl;
        std::cout << "Address of biggestBidder in setbiggestBidder: " << (biggestBidder) << std::endl;
        biggestBidder = offer;
        std::cout << "Address of biggestBidder in setbiggestBidder: " << (biggestBidder) << std::endl;
    }
    else
    {
    }
}

Oferta *Lote::getBiggestBidder()
{

    std::cout << "Address of biggestBidder in getBiggestBidder(): " << this->biggestBidder << std::endl;
    if (this->biggestBidder == nullptr)
    {
        std::cout << "biggestBidder IS NULL" << std::endl;
    }
    else
    {
        std::cout << "biggestBidder is NOT NULL" << std::endl;
    }

    return biggestBidder;
}

Now, the main and the problem:

Item l1(111, "BANANA");
Item l2(222, "MESA"); // this number 222 is just an ID for the item. It does nothing
Person p1("TheDude");
Offer of1(100, &p1);

// now put the item inside a vector and create an auction instance
std::vector<Item> item1;
collect.push_back(banana);
Auction coleccion1(collect, collect.size());


/*
Now we are ready. If I do this..
*/

std::cout << "Test with item outside vector" << std::endl;

std::cout << "Bidding $" << of1.getAmount() << " for item number " 
            << l2.getNumber() << ", " << l2.getItemName() << std::endl;

l2.setBiggestBidder(&of1); // amount 100
/*
If here I do 

l2.setBiggestBidder(&of2); // amount 200
l2.setBiggestBidder(&of1); // amount 100

At the bottom of the output, we get 200. It's all good.
*/
std::cout << l2.getBiggestBidder()->getAmount() << std::endl;

/* 
We get this output:
Test with item outside vector
Bidding $100 for item number 222, MESA
Address of offer in setbiggestBidder: 0x66fb20
Address of biggestBidder in setbiggestBidder: 0
Address of biggestBidder in setbiggestBidder: 0x66fb20
Address of biggestBidder in getBiggestBidder(): 0x66fb20
biggestBidder is NOT NULL
200 <-- l2.getBiggestBidder()->getAmount()
*/

// But if we try with the item inside the vector:

std::cout << "Bidding $" << of1.getAmount() << " for item number " 
                << coleccion1.getItemCollection().at(0).getNumber() << ", " 
                    << coleccion1.getItemCollection().at(0).getItemName() << std::endl;

/*
We get:
Test with item in vector
Bidding $100 for item number 111, BANANA
Address of offer in setbiggestBidder: 0x66fb10
Address of biggestBidder in setbiggestBidder: 0
Address of biggestBidder in setbiggestBidder: 0x66fb10
Address of biggestBidder in getBiggestBidder(): 0
biggestBidder IS NULL
0

and here is the issue.
*/

I really do not like having to do a post so large for a problem that I'm sure is about something extremely basic, but I have looked everywhere and cannot find any solution to this problem. It have been weeks since I stumbled upon this.

Why is that everything works fine if I put an item on the method setBiggestBidder, but the change does not survive the end of the method when I put an item that is inside of a vector? And how can I fix it?

Aucun commentaire:

Enregistrer un commentaire