C++ 类异常和运算符

C++ 类异常和运算符,c++,class,exception,namespaces,operators,C++,Class,Exception,Namespaces,Operators,我在让很多操作员工作时遇到了一些麻烦 作业说明如下 本任务的目的是处理例外情况。随你怎么说 回想一下,我为您提供了一个名为FlashDrive的示例类 已在下面绘制了图表。您可以获取源代码以访问 这里的FlashDrive类(.NET或.NET 2010)我希望您增强 该类,因此调用其方法或运算符可能会引发 异常,而不仅仅是将错误消息打印到cout。 目前,我们最喜欢的异常类是std::logic\u error。你可以 通过向其构造函数传递字符串值来创建逻辑错误。 正式来说,你也应该说“包括开

我在让很多操作员工作时遇到了一些麻烦

作业说明如下

本任务的目的是处理例外情况。随你怎么说 回想一下,我为您提供了一个名为FlashDrive的示例类 已在下面绘制了图表。您可以获取源代码以访问 这里的FlashDrive类(.NET或.NET 2010)我希望您增强 该类,因此调用其方法或运算符可能会引发 异常,而不仅仅是将错误消息打印到cout。 目前,我们最喜欢的异常类是std::logic\u error。你可以 通过向其构造函数传递字符串值来创建逻辑错误。 正式来说,你也应该说“包括开始工作” 逻辑错误,但Visual Studio(作为一个行为不好的孩子…) 让我们不带它就走吧。一旦一切正常, 驱动程序代码应按照类中的说明运行

虽然示例驱动程序代码可能不适用于所有这些 在以下情况下,我希望您抛出异常:

驱动器上放的东西超过了它的安全容量(由于 书面数据)

负数可能用作my_StorageUsed值(到期日) to运算符–或发送给构造函数调用的错误值)

负数可能用作my_StorageCapacity值 (由于运算符–或发送给构造函数调用的值不正确) 因此,请小心地通过所有的操作符和方法 类,以确保在每个 环境

我还希望您让操作员>为 类FlashDrive。最后,我想让你把FlashDrive放到 名称空间cs52

下面是我的代码和构建输出

FlashDrive.h

#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H
#include <iostream>
#include <cstdlib>

namespace cs52 {

class FlashDrive {
    friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2);
    friend FlashDrive operator- (FlashDrive used3, FlashDrive used4 );

public:

    FlashDrive& FlashDrive::operator=(int);
    FlashDrive::FlashDrive(int);
    FlashDrive& operator = (const FlashDrive& usedtotal){
        my_StorageUsed= usedtotal.my_StorageUsed;
        return *this;
    }
    FlashDrive( );
    FlashDrive( int capacity, int used, bool pluggedIn );

    void plugIn( );
    void pullOut( );
    void writeData( int amount );
    void eraseData( int amount );
    void formatDrive( );

    int  getCapacity( );
    void setCapacity( int amount );
    int  getUsed( );
    void setUsed( int amount );
    bool isPluggedIn( );

private:
    int my_StorageCapacity;   // in kilobytes
    int my_StorageUsed;       // in kilobytes
    bool my_IsPluggedIn;      // am I attached to a computer?
}extern drive1,drive2;

inline FlashDrive operator+ (FlashDrive used1, FlashDrive used2 ) {

    FlashDrive plus;

    plus.my_StorageUsed = (used1.getUsed()+ used2.getUsed());
    return plus;
}
inline bool operator< (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( lhs.getUsed() < rhs.getUsed() );
}
inline bool operator> (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( operator <( rhs, lhs ) );
}
inline FlashDrive operator - (FlashDrive used3, FlashDrive used4 ){
    FlashDrive minus;
    minus.my_StorageUsed = (used3.getUsed()- used4.getUsed());
    return minus;
};

}
#endif 
\ifndef FLASHDRIVE\u H
#定义FLASHDRIVE_H
#包括
#包括
名称空间cs52{
类闪存驱动器{
friend FlashDrive operator+(FlashDrive使用1,FlashDrive使用2);
朋友FlashDrive操作员-(FlashDrive使用3,FlashDrive使用4);
公众:
FlashDrive和FlashDrive::operator=(int);
FlashDrive::FlashDrive(int);
FlashDrive和operator=(const FlashDrive和usedtotal){
my_StorageUsed=usedtotal.my_StorageUsed;
归还*这个;
}
FlashDrive();
FlashDrive(int容量,int使用,bool pluggedIn);
void插件();
无效拉拔();
无效书面数据(整笔金额);
无效数据(整数金额);
void formatDrive();
int getCapacity();
无效设定容量(整数金额);
int getUsed();
使用的无效设置(整数金额);
bool-isPluggedIn();
私人:
int my_StorageCapacity;//以千字节为单位
int my_StorageUsed;//以千字节为单位
bool my_IsPluggedIn;//我是否连接到计算机?
}外部驱动器1、驱动器2;
内联FlashDrive操作员+(使用FlashDrive 1,使用FlashDrive 2){
FlashDrive plus;
plus.my_StorageUsed=(used1.getUsed()+used2.getUsed());
收益加成;
}
内联bool操作员<(FlashDrive和lhs、FlashDrive和rhs){
返回(lhs.getUsed()(FlashDrive和lhs、FlashDrive和rhs){

返回(操作员您需要为
类FlashDrive定义
操作员>>

friend std::istream& operator>>(std::istream& is, FlashDrive& fd)
{
  is >> my_StorageCapacity >> my_StorageUsed >> my_IsPluggedIn;
  return is;
}
类似的,您可以定义
操作符(std::ostream&os、const FlashDrive&fd)
{

os您尝试过什么?如果您的
FlashDrive
类在任何地方都没有指定操作员>>,那么错误消息有多奇怪?第一条错误消息告诉您没有定义操作员>
,这是正确的。请一步一步地处理错误消息,并询问更具体的问题,如果可能,请使用n SSCE()
#include <iostream>
#include <cstdlib>
#include "FlashDrive.h"
void main( )
{
using namespace cs52;
cs52::FlashDrive empty;
cs52::FlashDrive drive1( 10, 0, false );
cs52::FlashDrive drive2( 20, 0, false );

drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );

drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );

// read in a FlashDrive... 
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be read in
cs52::FlashDrive sample;
cin >> sample;

// print out a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;

cs52::FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;

cs52::FlashDrive other = combined – drive1;
cout << "the other cup's filled to " << other.getUsed( ) << endl;

if (combined > other) {
  cout << "looks like combined is bigger..." << endl;
}
else {
  cout << "looks like other is bigger..." << endl;
}

if (drive2 > other) {
  cout << "looks like drive2 is bigger..." << endl;
}
else {
  cout << "looks like other is bigger..." << endl;
}

if (drive2 < drive1) {
  cout << "looks like drive2 is smaller..." << endl;
}
else {
  cout << "looks like drive1 is smaller..." << endl;
}

// let's throw some exceptions...

try {
  empty = empty - combined;
  cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown... 
// so the lines of code here should
// be run, not the cout statement...
}

try {
  drive2.writeData( 10000 );
  cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown... 
// so the lines of code here should
// be run, not the cout statement...
}

try {
  cs52::FlashDrive f( -1, -1, false );
  cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown... 
// so the lines of code here should
// be run, not the cout statement...
}
}
friend std::istream& operator>>(std::istream& is, FlashDrive& fd)
{
  is >> my_StorageCapacity >> my_StorageUsed >> my_IsPluggedIn;
  return is;
}
std::ostream& operator>>(std::ostream& os, const FlashDrive& fd)
{
  os << fd.getCapacity() << fd.getUsed() << fd.isPluggedIn();
  return os;
}