首页 > 解决方案 > I cannot get the list to display the correct information input from the 3 argument constructor

问题描述

When I try to display my list of Stock objects it is not showing the information used with the 3 argument constructor. It is just showing a list with default information from the default constructor. How do I get it to show the information from the Stock objects?

Main function

#include <cstdlib>
#include "Stocks.h"
#include <list>


using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    list<Stocks> myList;
    Stocks stock1("Baker Hughes Inc.", "BKR", 24.86f);
    Stocks stock2("Schlumberger Technology Corp", "SLB", 34.20f);
    Stocks stock3("Halliburton Energy Services", "HAL", 23.94f);
    Stocks stock4("Amazon", "AMZN", 3373.63f);
    Stocks stock5("Microsoft", "MSFT", 259.29f);
    
    myList.push_front(stock1);
    myList.push_back(stock2);
    myList.push_back(stock3);
    myList.push_back(stock4);
    myList.push_back(stock5);

     for(std::list<Stocks>::iterator itr = myList.begin(); itr!= myList.end(); itr++)
        {
         Stocks stock = *itr;
         stock.display();
        }
    return 0;
}

Stocks class header file

#include <iostream>
#include <iomanip>

using namespace std;
#ifndef STOCKS_H
#define STOCKS_H

class Stocks {
public:
    string companyName;
    string tickerSymbol;
    float sharePrice;
    
    Stocks();
    Stocks(string companyName, string tickerSymbol, float sharePrice);
    Stocks(const Stocks& orig);
    void display();
    virtual ~Stocks();
private:

};

#endif /* STOCKS_H */

Stocks class source file

#include "Stocks.h"

Stocks::Stocks() {
    
    companyName = "";
    tickerSymbol = "";
    sharePrice = 0.0f;
}
Stocks::Stocks(string companyName, string tickerSymbol, float sharePrice)
{
    this->companyName = companyName;
    this->tickerSymbol = tickerSymbol;
    this->sharePrice = sharePrice;
}
Stocks::Stocks(const Stocks& orig) {
     
    
}
void Stocks::display()
{
    cout << setprecision(2) << fixed << endl;
    cout << "Company Name: " << companyName << endl;
    cout << "Ticker Symbol: " << tickerSymbol << endl;
    cout << "Share Price: "  << sharePrice << endl;
    
}
Stocks::~Stocks() {
}

标签: c++

解决方案


所以你重写你的复制构造函数Stocks(const Stocks& orig);什么都不做。因此,当您将 Stock 变量发送到数组时,将调用复制构造函数并且它什么也不做,因此要么创建一个实际返回对象副本的复制构造函数,要么根本不覆盖它这样的代码

class Stocks {
public:
    string companyName;
    string tickerSymbol;
    float sharePrice;

    Stocks();
    Stocks(string companyName, string tickerSymbol, float sharePrice);
   // Stocks(const Stocks& orig); <- its not needed
    void display();
    virtual ~Stocks();
private:

};
Stocks::Stocks() {
    companyName = "";
    tickerSymbol = "";
    sharePrice = 0.0f;
}
Stocks::Stocks(string companyName, string tickerSymbol, float sharePrice)
{
    this->companyName = companyName;
    this->tickerSymbol = tickerSymbol;
    this->sharePrice = sharePrice;
}
void Stocks::display()
{
    cout << setprecision(2) << fixed << endl;
    cout << "Company Name: " << companyName << endl;
    cout << "Ticker Symbol: " << tickerSymbol << endl;
    cout << "Share Price: " << sharePrice << endl;

}
Stocks::~Stocks() {
}
int main(int argc, char** argv) {
    list<Stocks> myList;
    Stocks stock1("Baker Hughes Inc.", "BKR", 24.86f);
    Stocks stock2("Schlumberger Technology Corp", "SLB", 34.20f);
    Stocks stock3("Halliburton Energy Services", "HAL", 23.94f);
    Stocks stock4("Amazon", "AMZN", 3373.63f);
    Stocks stock5("Microsoft", "MSFT", 259.29f);

    myList.push_front(stock1);
    myList.push_back(stock2);
    myList.push_back(stock3);
    myList.push_back(stock4);
    myList.push_back(stock5);

    for (std::list<Stocks>::iterator itr = myList.begin(); itr != myList.end(); itr++)
    {
        Stocks stock = *itr;
        stock.display();
    }
    return 0;
}

会运作良好。如果你想禁用对象的副本,你应该创建这样的东西

class Stocks {
public:
    string companyName;
    string tickerSymbol;
    float sharePrice;

    Stocks();
    Stocks(string companyName, string tickerSymbol, float sharePrice);
    //You shouldn't leave method empty
    //Stocks(const Stocks& orig);
    //the better way to do it is to just delete the method
    Stocks(const Stocks& orig) = delete;
    void display();
    virtual ~Stocks();
private:

};
Stocks::Stocks() {
    companyName = "";
    tickerSymbol = "";
    sharePrice = 0.0f;
}
Stocks::Stocks(string companyName, string tickerSymbol, float sharePrice)
{
    this->companyName = companyName;
    this->tickerSymbol = tickerSymbol;
    this->sharePrice = sharePrice;
}
//This part of code is not needed
//Stocks::Stocks(const Stocks& orig) {}
void Stocks::display()
{
    cout << setprecision(2) << fixed << endl;
    cout << "Company Name: " << companyName << endl;
    cout << "Ticker Symbol: " << tickerSymbol << endl;
    cout << "Share Price: " << sharePrice << endl;

}
Stocks::~Stocks() {
}
int main(int argc, char** argv) {
    list<Stocks*> myList;
    Stocks stock1("Baker Hughes Inc.", "BKR", 24.86f);
    Stocks stock2("Schlumberger Technology Corp", "SLB", 34.20f);
    Stocks stock3("Halliburton Energy Services", "HAL", 23.94f);
    Stocks stock4("Amazon", "AMZN", 3373.63f);
    Stocks stock5("Microsoft", "MSFT", 259.29f);

    myList.push_front(&stock1);
    myList.push_back(&stock2);
    myList.push_back(&stock3);
    myList.push_back(&stock4);
    myList.push_back(&stock5);

    for (std::list<Stocks*>::iterator itr = myList.begin(); itr != myList.end(); itr++)
    {
        Stocks* stock = *itr;
        stock->display();
    }
    return 0;
}

但这也不是最好的解决方案,因为列表将引用对象直到范围结束。


推荐阅读