1

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

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& 类型的返回值和相同类型的参数。提前致谢。

4

1 回答 1

3

当你声明一个数组时:

Stock stocks[STKS] = ...

数组元素通过0索引STKS - 1。当您stocks[STKS]在此行中访问时:

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

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

于 2013-11-09T18:17:38.810 回答