类异常和操作符
我很难让很多操作员工作
分配说明如下
这项任务的目的是处理例外情况。 您可能还记得,我已经为您提供了一个名为FlashDrive的示例类,该示例的示例如下。 您可以在此获取FlashDrive类的源代码(.NET或.NET 2010)我希望您能够增强此类,以便调用其方法或操作符可能会引发异常,而不仅仅是将错误消息打印到cout。 目前,我们最喜欢的异常类是std :: logic_error。 您可以通过将字符串值传递给其构造函数来创建logic_error。 正式的,你也应该说#include开始使用logic_error,但Visual Studio(作为一个表现不好的孩子......)让你离开它。 一旦你完全正确地工作,驱动程序代码应该按照课程中的描述运行。
虽然示例驱动程序代码可能不适用于所有这些情况,但我希望在下列情况下引发异常:
更多的东西已经放在驱动器上比它可以安全地保存(由于writeData)
负数可能被用作my_StorageUsed值(由于运算符 - 或者将错误值发送给构造函数调用)
负数可能被用作my_StorageCapacity值(由于操作符 - 或者将错误的值发送给构造函数调用)所以请仔细考虑所有类的运算符和方法,以确保在这些情况下引发logic_error。
我也想让你的操作符<<和operator >>为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
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 );
}
}
Main.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 ------ Build开始7/29/2013 4:48:43 AM。 InitializeBuildStatus:触摸“Debug FlashDriver.unsuccessfulbuild”。 ClCompile:Main.cpp c: documents and settings administrator my documents visual studio 2010 projects flashdriver flashdriver main.cpp(28):error C2678:binary'>>':找不到操作符, (或没有可接受的转换)c: program files microsoft visual studio 10.0 vc include istream(1053):可能是'std :: basic_istream <_Elem,_Traits >&std ::运算符
>(_Elem = char,_Traits = std :: char_traits)>(std :: basic_istream <_Elem,_Traits> &&,signed char *)'c: program files microsoft visual studio 10.0 vc include istream(1060) :或'std :: basic_istream <_Elem,_Traits>&std :: operator>(std :: basic_istream <_Elem,_Traits> &&,signed char&)'与[_Elem = char,_Traits = std :: char_traits] c:程序文件 microsoft visual studio 10.0 vc include istream(1067):or'std :: basic_istream <_Elem,_Traits>&std :: operator>(std :: basic_istream <_Elem,_Traits> &&,unsigned char *)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(1074):or'std :: basic_istream <_Elem,_Traits>&std :: operator>( std :: basic_istream <_Elem,_Traits> &&,unsigned char&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(1097):or 'std :: basic_istream <_Elem,_Traits>&std :: operator>(std :: basic_istream <_Elem,_Traits>&,signed char *)'[_Elem = char,_Traits = std :: char_t (std :: basic_istream <_Elem,_Traits>&),这是一个非常有用的工具, '_Elem,_Traits和&std :: char_traits]与 [std :: char_traits]签署的字符&)与C: program files microsoft visual studio 10.0 vc include istream(1111):or'std :: basic_istream <_Elem,_Traits>&std :: operator>(std :: basic_istream <_Elem,_Traits>&,unsigned char *)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream (1118):或[_Elem = char,_Traits = std :: char_traits]或'std :: basic_istream <_Elem,_Traits>&std :: operator>(std :: basic_istream <_Elem,_Traits>&,unsigned char&) c: program files microsoft visual studio 10.0 vc include istream(1128):or'std :: basic_istream <_Elem,_Traits>&std :: operator,cs52 :: FlashDrive>(std :: basic_istream <_Elem,_Traits > &&,_ Ty&)'[_Elem = char,_Traits = std :: char_traits,_Ty = cs52 :: FlashDrive] c: program files microsoft visual studio 10.0 vc include istream(179):or std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator(std :: basic_istream <_Elem,Traits>&(_ cdecl *)(std :: basic_istream < _Elem,_Traits>&))'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(185):or'std :: basic_istream <_Elem, _Traits>&std :: basic_istream <_Elem,_Traits> ::运算符(std :: basic_ios <_Elem,traits>&(_cdecl *)(std :: basic_ios <_Elem,_Traits>&))'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(192):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator> >(std :: ios_base&(__ cdecl *)(std :: ios_base&))'[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream 199)或'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(std :: _ Bool&)'与[_Elem = char,_Traits = std :: char_traits] c : program files microsoft visual Studio 10.0 vc include istream(218):或者'_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(253):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator> >(unsigned short&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(272):or'std :: basic_istream <_Elem,_Traits >&std :: basic_istream <_Elem,_Traits> :: operator >>(int&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream 298):或[std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(unsigned int&)'与[_Elem = char,_Traits = std :: char_traits] c:程序文件 microsoft visual studio 10.0 vc include istream(316):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(long &')与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(334):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(unsigned long&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(354):或'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,traits> :: operator >>(_ int64&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft Visual studio 10.0 vc include istream(373):或'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(unsigned __int64&)'with [_Elem = char, _Trans =&std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(392) >>(float&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(411):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(double&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(429):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>(long double&)'with [_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(447):or'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: operator >>( void *&)'[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include istream(466):or'std :: basic_istream <_Elem,_Traits>&std ::尝试匹配参数列表时,[_Elem = char,_Traits = std :: char_traits]时的:: basic_istream <_Elem,_Traits> ::运算符(std :: basic_streambuf <_Elem,_Traits> *)'(std :: istream ,cs52 :: FlashDrive)'c: documents and settings administrator my documents visual studio 2010 projects flashdriver flashdriver main.cpp(33 ):错误C2679:二进制'<<':找不到操作符找到类型为'cs52 :: FlashDrive'的右操作数(或者没有可接受的转换)c: program files microsoft visual studio 10.0 vc 包括 ostream(679):可以是'std :: basic_ostream <_Elem,_Traits>&std :: operator <<>(std :: basic_ostream <_Elem,_Traits>&,const char *)'[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(726):or'std :: basic_ostream <_Elem,_Traits>&std :: operator <<>(std :: basic_ostream < _Elem,_Traits =&,char)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(764):or'std :: basic_ostream <_Elem ,_Traits>&std :: operator <<>(std :: basic_ostream <_Elem,_Traits>&,const char *)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(811):or'std :: basic_ostream <_Elem,_Traits>&std :: operator <<>(std :: basic_ostream <_Elem,_Traits>&,char)' lem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(937):or'std :: basic_ostream <_Elem,_Traits>&std :: operator <<>( std :: basic_ostream <_Elem,_Traits>&,const signed char *)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(944):或者'std :: basic_ostream <_Elem,_Traits>&std :: operator <<>(std :: basic_ostream <_Elem,_Traits>&,signed char)'[_Elem = char,_Traits = std :: char_traits] c:程序文件 microsoft visual studio 10.0 vc include ostream(951):或'std :: basic_ostream <_Elem,_Traits>&std :: operator <<>(std :: basic_ostream <_Elem,_Traits>&,const unsigned char *)'[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(958):or'std :: basic_ostream <_Elem,_Traits>&std ::运算符<<>(std :: basic_ostream <_Elem,_Traits>&,unsigned char)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 v c include ostream(968):or'std :: basic_ostream <_Elem,_Traits>&std :: operator <<,cs52 :: FlashDrive>(std :: basic_ostream <_Elem,_Traits> &&,_ Ty)'与[_Elem = char,_Traits = std :: char_traits,_Ty = cs52 :: FlashDrive] c: program files microsoft visual studio 10.0 vc include ostream(1085):or'std :: basic_ostream <_Elem,_Traits>&std: :operator <<>(std :: basic_ostream <_Elem,_Traits>&,const std :: error_code&)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(186):or std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(std :: basic_ostream <_Elem,Traits>&(_ cdecl *)(std ):'_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(192):or'std :: :: basic_ostream <_Elem,_Traits>&))' basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(std :: basic_ios <_Elem,Traits>&(_cdecl *)(std :: basic_ios <_Elem,_Traits>&))' [_Elem = char,_Traits = std :: ch ar_traits] c: program files microsoft visual studio 10.0 vc include ostream(199):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(std: :ios_base&(__ cdecl *)(std :: ios_base&))'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(206):or 'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(std :: _ Bool)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(226):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(short)'with [_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(260):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator < <(unsigned short)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(280):or'std :: basic_ostrea m <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(int)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc 使用[_Elem = char,_Traits = std :: char_traits]包含 ostream(305):或'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(unsigned int) c: program files microsoft visual studio 10.0 vc include ostream(325):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(long)'with [_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(345):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem, _Traits> :: operator <<(unsigned long)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(366):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,traits> :: operator <<(_ int64)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsof t视觉工作室10.0 vc include ostream(386):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(unsigned __int64)'with [_Elem = char, _traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(407):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(float)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(427):or'std :: basic_ostream <_Elem,_Traits> &std :: basic_ostream <_Elem,_Traits> :: operator <<(double)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(447) :或'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(long double)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(467):or'std :: basic_ostream <_Elem,_Traits>&std :: basic_ostream <_Elem,_Traits> :: operator <<(const void *)'与[_Elem = char,_Traits = std :: char_traits] c: program files microsoft visual studio 10.0 vc include ostream(487):or'std :: basic_ostream <_Elem,_Traits>&std: :尝试匹配参数列表时,使用[_Elem = char,_Traits = std :: char_traits]时,basic_ostream <_Elem,_Traits> :: operator <<(std :: basic_streambuf <_Elem,_Traits> *)''(std :: ostream,cs52 :: FlashDrive)'c: documents and settings administrator my documents visual studio 2010 projects flashdriver flashdriver main.cpp(38):error C2146:syntax error:missing';' 之前标识符' - 'c:文件和设置管理员我的文档 visual studio 2010 projects flashdriver flashdriver main.cpp(38):错误C2065:' - ':未声明的标识符c: documents and settings 管理员我的文档 visual studio 2010 项目 flashdriver flashdriver main.cpp(38):错误C2146:语法错误:缺少';' 在标识符'drive1'之前FlashDrive.cpp生成代码...
构建失败。
时间经过00:00:01.82 ==========构建:0成功,1失败,0最新,0跳过==========
您需要为class FlashDrive
定义operator>>
friend std::istream& operator>>(std::istream& is, FlashDrive& fd)
{
is >> my_StorageCapacity >> my_StorageUsed >> my_IsPluggedIn;
return is;
}
类似的,你可以定义operator<<
std::ostream& operator>>(std::ostream& os, const FlashDrive& fd)
{
os << fd.getCapacity() << fd.getUsed() << fd.isPluggedIn();
return os;
}
链接地址: http://www.djcxy.com/p/68429.html