gpt4 book ai didi

c++ - 段错误重载运算符 <<

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:45 25 4
gpt4 key购买 nike

我的代码编译正常。但是当我尝试使用我的重载运算符时<< - 应用程序崩溃。我自己没有找到任何决定。如果我不使用此运算符,一切正常。例如,我可以将负参数传递给构造函数,它将使用类数据指针“company”显示一条消息。调试器显示程序在此行崩溃:

os << "Company: " << s.company

在函数中:

std::ostream& operator<<(std::ostream& os, const Stock& s)
{
using std::ios_base;
// set format to #.###
ios_base::fmtflags orig =
os.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = os.precision(3);

os << "Company: " << s.company
<< " Shares: " << s.shares << '\n';
os << " Share Price: $" << s.share_val;
// set format to #.##
os.precision(2);
os << " Total Worth: $" << s.total_val << '\n';

// restore original format
os.setf(orig, ios_base::floatfield);
os.precision(prec);
return os;
}

代码如下:

// stock20.h -- augmented version
#ifndef STOCK20_H_
#define STOCK20_H_
#include <iostream>
class Stock
{
private:
char* company;
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock(); // default constructor
Stock(const char* co, long n = 0, double pr = 0.0);
~Stock(); // do-nothing destructor
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
const Stock & topval(const Stock & s) const;
friend std::ostream& operator<<(std::ostream& os, const Stock& s);
};

#endif

实现:

// stock20.cpp -- augmented version
#include "stock20.h"
#include <cstring>

#define my_delete(x){ delete[] x; x = NULL; }
using namespace std;
// constructors
Stock::Stock() // default constructor
{
company = new char[1];
company[0] = '\0';
shares = 0;
share_val = 0.0;
total_val = 0.0;
}

Stock::Stock(const char* co, long n, double pr)
{
company = new char[strlen(co)+1];
strcpy(company,co);

if (n < 0)
{
std::cout << "Number of shares can't be negative; "
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}

// class destructor
Stock::~Stock() // quiet class destructor
{
my_delete(company);
}

// other methods
void Stock::buy(long num, double price)
{
if (num < 0)
{
std::cout << "Number of shares purchased can't be negative. "
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}

void Stock::sell(long num, double price)
{
using std::cout;
if (num < 0)
{
cout << "Number of shares sold can't be negative. "
<< "Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can't sell more than you have! "
<< "Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}

void Stock::update(double price)
{
share_val = price;
set_tot();
}

std::ostream& operator<<(std::ostream& os, const Stock& s)
{
using std::ios_base;
// set format to #.###
ios_base::fmtflags orig =
os.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = os.precision(3);

os << "Company: " << s.company
<< " Shares: " << s.shares << '\n';
os << " Share Price: $" << s.share_val;
// set format to #.##
os.precision(2);
os << " Total Worth: $" << s.total_val << '\n';

// restore original format
os.setf(orig, ios_base::floatfield);
os.precision(prec);
return os;
}

const Stock & Stock::topval(const Stock & s) const
{
if (s.total_val > total_val)
return s;
else
return *this;
}

以及代码用法:

// usestok2.cpp -- using the Stock class
// compile with stock20.cpp
#include "stock20.h"

const int STKS = 4;
int main()
{{
//create an array of initialized objects
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.0),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Obelisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};

std::cout << "Stock holdings:\n";
int st;
for (st = 0; st < STKS; st++)
std::cout<<stocks[STKS]; //here we got an error
// set pointer to first element
const Stock * top = &stocks[0];
for (st = 1; st < STKS; st++)
top = &top->topval(stocks[st]);
// now top points to the most valuable holding
std::cout << "\nMost valuable holding:\n";
std::cout<<*top;}
// std::cin.get();
return 0;
}

既然我问了一个问题,我希望你不介意我再问一个。我怎样才能避免在标题中使用包含。例如,对于重载的 operator<< 我需要包含 iostream,因为它具有 ostream& 类型的返回值和相同类型的参数。提前致谢。

最佳答案

当你声明一个数组时:

Stock stocks[STKS] = ...

数组元素索引0通过STKS - 1 .当您访问 stocks[STKS]在这一行中:

std::cout<<stocks[STKS]; // Out-of-bounds: STKS > STKS - 1

您正在访问一个不存在的数组元素,这导致了崩溃。鉴于上下文,您可能需要 std::cout<<stocks[st];相反(帽子提示:@gx_)。

关于c++ - 段错误重载运算符 <<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com