Can't change unique_ptr data with new information!

Hey yall I was hoping to get help with this program I have written. This is to create DVD objects and prompt the user for DVD information. This information is then stored in a vector. The issue I am having is trying to change the data of a ptr in the vector. I can call a global function, input the information but when I display the vector again it shows the original data inputted. If anyone could please help that would be great, I have been stuck on this for awhile.

// DVD claas declaration w/include guards
//DVD.h

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <iomanip>
using namespace std;

#ifndef DVD_H
#define DVD_H
class DVD
{

public:

DVD();

void LoadInformation();

void SetTitle(string NewTitle);

string GetTitle();

void SetType(string NewType);

string GetType();

void SetCost(float NewCost);

float GetCost();

private:

string Title;
string CDType;
float Cost = 0.00;
void ListValidDVDTypes()
{
cout << "Select DVD Type from menu below.\n";
cout << "1. Game" << endl;
cout << "2. Word" << endl;
cout << "3. Compiler" << endl;
cout << "4. Spreadsheet" << endl;
cout << "5. DBase" << endl;
cout << "6. Presentation" << endl;
}

};
#endif

// Implementations of DVD class member functions
//DVD.cpp

#include"DVD.h"


DVD::DVD()
{
Title = "No Title";
CDType = "Game";
Cost = 0.00;
}

void DVD::LoadInformation()
{
int choice = 0;

cout << "DVD object created." << endl;
cout << "Enter DVD Title: ";
cin >> Title;
ListValidDVDTypes();
cout << "Enter number that corresponds to DVD type (1-6): ";
cin >> choice;

switch (choice)
{
case 1:
choice = 1;
CDType = "Game";
break;

case 2:
choice = 2;
CDType = "Word";
break;

case 3:
choice = 3;
CDType = "Compiler";
break;

case 4:
choice = 4;
CDType = "Spreadsheet";
break;

case 5:
choice = 5;
CDType = "DBase";
break;

case 6:
choice = 6;
CDType = "Presentation";
break;

}

cout << "Please enter the cost of the DVD: $";
cin >> Cost;
cout << "\n";

}

void DVD::SetTitle(string NewTitle)
{
Title = NewTitle;
}

string DVD::GetTitle()
{
return Title;
}

void DVD::SetType(string NewType)
{
CDType = NewType;
}

string DVD::GetType()
{
return CDType;
}

void DVD::SetCost(float NewCost)
{
Cost = NewCost;
}

float DVD::GetCost()
{
return Cost;
}

//Global function prototype
//DisplayGlobalFunctions.h

#include "DVD.h"

void Display_DVD_Information(vector<unique_ptr<DVD>> & DVDs);

void DisplayTotalAndAverageCosts(vector<unique_ptr<DVD>> &DVDs);

void ChangeDVDData(unique_ptr<DVD> &aDVD);


//Implementation of Global Functions
//Display_Global_Functions.cpp
#include "DisplayGlobalFunctions.h"

void Display_DVD_Information(vector<unique_ptr<DVD>> & DVDs)
{
for (int i = 0; i < DVDs.size() - 2; ++i)
{
cout << "DVD 1 Information" << endl;
cout << "Title: " << DVDs[0]->GetTitle() << endl;
cout << "Type: " << DVDs[0]->GetType() << endl;
cout << "Cost: $" << DVDs[0]->GetCost() << "\n" << endl;

cout << "DVD 2 Information" << endl;
cout << "Title: " << DVDs[1]->GetTitle() << endl;
cout << "Type: " << DVDs[1]->GetType() << endl;
cout << "Cost: $" << DVDs[1]->GetCost() << "\n" << endl;

cout << "DVD 3 Information" << endl;
cout << "Title: " << DVDs[2]->GetTitle() << endl;
cout << "Type: " << DVDs[2]->GetType() << endl;
cout << "Cost: $" << DVDs[2]->GetCost() << endl;
}

}
void DisplayTotalAndAverageCosts(vector<unique_ptr<DVD>> &DVDs)
{
float total = DVDs[0]->GetCost() + DVDs[1]->GetCost() + DVDs[2]->GetCost();
float average = (DVDs[0]->GetCost() + DVDs[1]->GetCost() + DVDs[2]->GetCost()) / 3;

cout << "\n";
cout << "DVD1 Cost: $" << DVDs[0]->GetCost() << endl;
cout << "DVD2 Cost: $" << DVDs[1]->GetCost() << endl;
cout << "DVD2 Cost: $" << DVDs[2]->GetCost() << endl;
cout << "\n";
cout << "The total cost of all 3 DVDs are: $" << total << "\n" << endl;
cout << "The average of cost of all 3 DVDs are: $" << average << "\n" << endl;

}
void ChangeDVDData(unique_ptr<DVD> &aDVD)
{
int changeDVD = 0;

cin >> changeDVD;

switch (changeDVD)
{
case 1:
{
changeDVD = 1;
cout << "Enter new DVD Information" << endl;
aDVD.release();
aDVD.reset(new DVD);
aDVD->LoadInformation();
break;
}

case 2:
{
changeDVD = 2;
cout << "You have made no changes!\n" << endl;
break;
}


}

}


//Test driver for DVD class
//Lab1.cpp

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <iomanip>
#include "DVD.h"
#include "DisplayGlobalFunctions.h"
using namespace std;

void Display_DVD_Information(vector<unique_ptr<DVD>> & DVDs);
void DisplayTotalAndAverageCosts(vector<unique_ptr<DVD>> &DVDs);
void ChangeDVDData(unique_ptr<DVD> &aDVD);

int main()
{
cout << "Welcome to the DVD program!" << endl;

DVD::DVD();

vector<unique_ptr<DVD>> DVD_Container;

unique_ptr<DVD> pDVD1(new DVD);
assert(pDVD1 != NULL);
unique_ptr<DVD> pDVD2(new DVD);
assert(pDVD2 != NULL);
unique_ptr<DVD> pDVD3(new DVD);
assert(pDVD3 != NULL);

pDVD1->LoadInformation();
cout << "Title: " << pDVD1->GetTitle() << endl;
cout << "Type: " << pDVD1->GetType() << endl;
cout << "Cost: $" << pDVD1->GetCost() << endl;
DVD_Container.push_back(std::move(pDVD1));
cout << "DVD1 Object Created!\n" << endl;

pDVD2->LoadInformation();
cout << "Title: " << pDVD2->GetTitle() << endl;
cout << "Type: " << pDVD2->GetType() << endl;
cout << "Cost: $" << pDVD2->GetCost() << endl;
DVD_Container.push_back(std::move(pDVD2));
cout << "DVD2 Object Created!\n" << endl;

pDVD3->LoadInformation();
cout << "Title: " << pDVD3->GetTitle() << endl;
cout << "Type: " << pDVD3->GetType() << endl;
cout << "Cost: $" << pDVD3->GetCost() << endl;
DVD_Container.push_back(std::move(pDVD3));
cout << "DVD3 Object Created!\n" << endl;

Display_DVD_Information(DVD_Container);

DisplayTotalAndAverageCosts(DVD_Container);

cout << "Current DVD1 Information" << endl;
cout << "Title: " << DVD_Container[0]->GetTitle() << endl;
cout << "Type: " << DVD_Container[0]->GetType() << endl;
cout << "Cost: " << DVD_Container[0]->GetCost() << endl;
cout << "Would you like to change DVD1 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD1);

cout << "Current DVD2 Information" << endl;
cout << "Title: " << DVD_Container[1]->GetTitle() << endl;
cout << "Type: " << DVD_Container[1]->GetType() << endl;
cout << "Cost: " << DVD_Container[1]->GetCost() << endl;
cout << "Would you like to change DVD2 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD2);


cout << "Current DVD3 Information" << endl;
cout << "Title: " << DVD_Container[2]->GetTitle() << endl;
cout << "Type: " << DVD_Container[2]->GetType() << endl;
cout << "Cost: " << DVD_Container[2]->GetCost() << endl;
cout << "Would you like to change DVD3 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD3);

Display_DVD_Information(DVD_Container);

DisplayTotalAndAverageCosts(DVD_Container);

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// ...

Display_DVD_Information(DVD_Container);

DisplayTotalAndAverageCosts(DVD_Container);

/*
cout << "Current DVD1 Information" << endl;
cout << "Title: " << DVD_Container[0]->GetTitle() << endl;
cout << "Type: " << DVD_Container[0]->GetType() << endl;
cout << "Cost: " << DVD_Container[0]->GetCost() << endl;
cout << "Would you like to change DVD1 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD1);

cout << "Current DVD2 Information" << endl;
cout << "Title: " << DVD_Container[1]->GetTitle() << endl;
cout << "Type: " << DVD_Container[1]->GetType() << endl;
cout << "Cost: " << DVD_Container[1]->GetCost() << endl;
cout << "Would you like to change DVD2 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD2);


cout << "Current DVD3 Information" << endl;
cout << "Title: " << DVD_Container[2]->GetTitle() << endl;
cout << "Type: " << DVD_Container[2]->GetType() << endl;
cout << "Cost: " << DVD_Container[2]->GetCost() << endl;
cout << "Would you like to change DVD3 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD3);
*/

for( std::size_t i = 0 ; i < DVD_Container.size() ; ++i )
{
    cout << "Current DVD" << i+1 << " Information" << endl;
    cout << "Title: " << DVD_Container[i]->GetTitle() << endl;
    cout << "Type: " << DVD_Container[i]->GetType() << endl;
    cout << "Cost: " << DVD_Container[i]->GetCost() << endl;
    cout << "Would you like to change DVD" << i+1 << " data?(1 = Yes, 2 = No): ";

    // *** note *** to modify a unique_ptr<> which is in the container,
    //              pass the reference to the unique_ptr<> in the container
    ChangeDVDData( DVD_Container[i] );
}


Display_DVD_Information(DVD_Container);

// ... 
LOL thankyou so much, I was stuck for a minute. I had actually tried using a for loop in main using reset, erase and release. Hard to find examples to a specific situation. So just to make sure I understand, bc I think this is where I get a bit confused with pointers. I am not passing the unique_ptr pDVD1 down to ChangeDVDData, but passing the body of the global function up to main, does that sound about right?
> I am not passing the unique_ptr pDVD1 down to ChangeDVDData,

Yes. Now you are not.


> but passing the body of the global function up to main, does that sound about right?

No. You are calling the function, passing the reference to a unique_ptr<> in DVD_Container.

1
2
ChangeDVDData( DVD_Container[i] ); // call function ChangeDVDData(),
                                   // pass reference to DVD_Container[i] 


Illustration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <memory>
#include <vector>

void dump( std::unique_ptr<int>& p, const char* name )
{
    std::cout <<'\'' << name << "' std::unique_ptr<int> at address: "
               << std::addressof(p) << "\n\tpoints to int at address: " << p.get() ;

    if(p) std::cout << " which has the value: " << *p  << "\n\n" ;
    else std::cout << " (nullptr)\n\n" ;
}

int main()
{
     std::vector< std::unique_ptr<int> > vec ;

     std::unique_ptr<int> temp( new int(999) ) ;
     dump( temp, "temp" ) ;

     // move the unique_ptr to the vector
     vec.push_back( std::move(temp) ) ;
     std::cout << "temp has been moved to vec[0]\n\n" ;

     // temp and vec[0] are two different unique_ptr<int> instances.
     dump( temp, "temp" ) ; // temp is now a nullptr
     dump( vec[0], "vec[0]" ) ; // vec[0] points to the integer

     // conclusion: modifying temp will not modify vec[0]
     std::cout << "modify temp\n\n" ;
     temp.reset( new int (-77) ) ;
     dump( temp, "temp" ) ; // temp now points to int -77
     dump( vec[0], "vec[0]" ) ; // vec[0] remains unchanged
}

http://coliru.stacked-crooked.com/a/7f8e424496206ef8
Oh ok I got ya, appreciate the illustration too. Thanks again you've been a big help.
Topic archived. No new replies allowed.