c++ - 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() {
}
解决方案
所以你重写你的复制构造函数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;
}
但这也不是最好的解决方案,因为列表将引用对象直到范围结束。
推荐阅读
- css - 在 VStudio MVC 5 应用程序的 html 页面中引用 css 不起作用
- javascript - """ 之间的 Javascript 正则表达式文本
- terraform - 如何在 Terraform 中初始化远程状态?
- jquery - 使用非类选择器选择输入类型
- jetbrains-ide - 如何在 GoLand 终端中使用 IDE 键盘映射
- oauth-2.0 - AWS Cognito - 在授权访问 api 网关时使用范围
- java - Python多线程执行多个jar文件需要更长的时间
- gitlab - 如何将现有的 gitlab 项目复制到新项目,但没有任何代码 - 只有它的设置?
- python - Django CASCADE 和 post_delete 交互
- sql - T-SQL UPDATE 语句影响的记录少于 select 语句