当前位置: 首页 > 知识库问答 >
问题:

类异常和运算符

金嘉
2023-03-14

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

作业说明如下

本任务的目的是处理例外情况。您可能还记得,我为您提供了一个名为FlashDrive的示例类,如下图所示。您可以在此处获取FlashDrive类的源(.NET或.NET 2010)。我希望您增强该类,以便调用其方法或运算符可能引发异常,而不仅仅是将错误消息打印到cout。目前,我们最喜欢的异常类是std::logic\u error。您可以通过向其构造函数传递字符串值来创建逻辑错误。正式来说,你也应该说#include开始处理逻辑错误,但是visualstudio(作为一个行为不好的孩子…)让我们不带它就走吧。一旦一切正常运行,驱动程序代码应该按照类中的描述运行。

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

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

一个负数可能用作my_StorageUsed值(由于运算符–或发送给构造函数调用的坏值)

负数可能用作my_StorageCapacity值(由于运算符–或发送给构造函数调用的坏值),因此请小心地遍历类的所有运算符和方法,以确保在每种情况下都抛出逻辑_错误。

我还想请你们接接线员

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

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 

FlashDrive。cpp

#include <iostream>
#include <cstdlib>
#include "FlashDrive.h"

namespace cs52 {

FlashDrive::FlashDrive( ) {
  my_StorageCapacity = 0;
  my_StorageUsed = 0;
  my_IsPluggedIn = false;
}
FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn ) { 
  my_StorageCapacity = capacity;
  my_StorageUsed = used;
  my_IsPluggedIn = pluggedIn;
}
void FlashDrive::plugIn( ) {
  my_IsPluggedIn = true;
}
void FlashDrive::pullOut( ) {
  my_IsPluggedIn = false;
}
void FlashDrive::writeData( int amount ) {
  my_StorageUsed += amount;
}
void FlashDrive::eraseData( int amount ) {
  my_StorageUsed -= amount;
}
void FlashDrive::formatDrive( ) {
  my_StorageUsed = 0;
}

int  FlashDrive::getCapacity( ) {
  return( my_StorageCapacity );
}
void FlashDrive::setCapacity( int amount ) {
  my_StorageCapacity = amount;
}
int  FlashDrive::getUsed( ) {
  return( my_StorageUsed );
}
void FlashDrive::setUsed( int amount ) {
  my_StorageUsed = amount;
}
bool FlashDrive::isPluggedIn( ) {
  return( my_IsPluggedIn );
}
}

主要的cpp

#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...
}
}

这是一个非常奇怪的错误信息

------构建开始:项目:FlashDriver,配置:调试Win32------构建开始于2013年7月29日上午4:48:43。InitializeBuildStatus:触摸“调试\ FlashDriver.unsuccessfulbuild”。ClCompile:Main。cpp c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main。cpp(28):错误C2678:二进制'

生成失败。

时间流逝00:00:01.82 ========== 构建:0成功,1失败,0最新,0跳过 ==========

共有1个答案

寇涵容
2023-03-14

您需要定义运算符

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;
}

 类似资料:
  • 通常,例外是任何异常情况。 例外通常表示错误,但有时他们故意放入程序,例如提前终止程序或从资源短缺中恢复。 有许多内置异常,表示读取文件末尾或除以零等条件。 我们可以定义自己的异常,称为自定义异常。 异常处理使您可以优雅地处理错误并对其执行有意义的操作。 异常处理有两个组成部分:“抛出”和“捕获”。 Identifying Exception (Errors) Python中发生的每个错误都会导致

  • 本文向大家介绍c异或运算 c异或运算符号,包括了c异或运算 c异或运算符号的使用技巧和注意事项,需要的朋友参考一下 与运算:& 两者都为1为1,否则为0 1&1=1,  1&0=0,  0&1=0,  0&0=0 或运算:| 两者都为0为0,否则为1 1|1 = 1,  1|0 = 1,  0|1 = 1, 0|0 = 0 非运算:~ 1取0,0取1 ~1 = 0, ~0 = 1 ~(10001)

  • 以下代码返回此错误: 主要的cpp | 80 |错误:将“const matrix”作为“T”的“this”参数传递 有什么想法为什么以及如何修复它吗? http://ideone.com/5OigTP

  • 当面对选择抛出异常的类型时,您可以使用由别人编写的异常 - Java平台提供了许多可以使用的异常类 - 或者您可以编写自己的异常类。 如果您对任何以下问题回答“是”,您应该编写自己的异常类;否则,你可以使用别人的。 你需要一个Java平台中没有表示的异常类型吗? 如果用户能够区分你的异常与由其他供应商编写的类抛出的异常吗? 你的代码是否抛出不止一个相关的异常? 如果您使用他人的例外,用户是否可以访

  • 当发生像数组下标越界或类型断言失败这样的运行错误时,Go 运行时会触发运行时 panic,伴随着程序的崩溃抛出一个 runtime.Error 接口类型的值。这个错误值有个 RuntimeError() 方法用于区别普通错误。 panic 可以直接从代码初始化:当错误条件(我们所测试的代码)很严苛且不可恢复,程序不能继续运行时,可以使用 panic 函数产生一个中止程序的运行时错误。panic 接

  • 一、值,类型和运算符 在机器的表面之下,程序在运转。 它不费力就可以扩大和缩小。 在和谐的关系中,电子散开并重新聚合。 监视器上的表格只是水面上的涟漪。 本质隐藏在下面。 Master Yuan-Ma,《The Book of Programming》 计算机世界里只有数据。 你可以读取数据,修改数据,创建新数据 - 但不能提及不是数据的东西。 所有这些数据都以位的长序列存储,因此基本相似。 位是