Classes Exceptions and Operators

I am having some trouble getting a lot of the operators to work

The instruction for the assignment is as follows

The purpose of this assignment is to work with exceptions. As you may recall, I have provided you with a sample class named FlashDrive which has been diagrammed below. You can acquire the source to the FlashDrive class here ( .NET or .NET 2010) I'd like you to enhance this class so that invoking its methods or operators potentially throw exceptions, rather than just printing error messages to cout. Currently, our favorite exception class is std::logic_error. You can create a logic_error by passing a string value to its constructor. Officially, you should also say #include to begin working with logic_error, but Visual Studio (being a badly behaved child...) let's you get away without it. Once you get it all working correctly, the driver code should run as described in class.

Although the sample driver code might not code for all these circumstances, I would like you to throw exceptions when:

more stuff has been put onto the drive than it can safely hold (due to writeData)

a negative number is potentially used as a my_StorageUsed value (due to operator – or bad values being sent to the constructor call)

a negative number is potentially used as a my_StorageCapacity value (due to operator – or bad values being sent to the constructor call) So carefully wind your way thru all the operators and methods of the class ensuring that logic_error gets thrown in each of these circumstances.

I'd also like you to get operator << and operator >> working for the class FlashDrive. And finally, I'd like you to place FlashDrive into the namespace cs52.

Below is my code and build output

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

And here a very strange error message i'm getting

------ Build started: Project: FlashDriver, Configuration: Debug Win32 ------ Build started 7/29/2013 4:48:43 AM. InitializeBuildStatus: Touching "DebugFlashDriver.unsuccessfulbuild". ClCompile: Main.cpp c:documents and settingsadministratormy documentsvisual studio 2010projectsflashdriverflashdrivermain.cpp(28): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) c:program filesmicrosoft visual studio 10.0vcincludeistream(1053): could be 'std::basic_istream<_Elem,_Traits> &std::operator

>(std::basic_istream<_Elem,_Traits> &&,signed char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1060): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &&,signed char &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1067): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &&,unsigned char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1074): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &&,unsigned char &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1097): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &,signed char *)' with [ _Elem=char, _Traits=std::char_t raits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1104): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &,signed char &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1111): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &,unsigned char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1118): or 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &,unsigned char &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(1128): or 'std::basic_istream<_Elem,_Traits> &std::operator ,cs52::FlashDrive>(std::basic_istream<_Elem,_Traits> &&,_Ty &)' with [ _Elem=char, _Traits=std::char_traits, _Ty=cs52::FlashDrive ] c:program filesmicrosoft visual studio 10.0vcinclude 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> &))' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(185): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator (std::basic_ios<_Elem,Traits> &(_cdecl *)(std::basic_ios<_Elem,_Traits> &))' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(192): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(199): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(218): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(253): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(272): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(298): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(316): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(334): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(354): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,Traits>::operator >>(_int64 &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(373): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(392): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(411): or 'std ::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(429): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(447): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeistream(466): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator (std::basic_streambuf<_Elem,_Traits> *)' with [ _Elem=char, _Traits=std::char_traits ] while trying to match the argument list '(std::istream, cs52::FlashDrive)' c:documents and settingsadministratormy documentsvisual studio 2010projectsflashdriverflashdrivermain.cpp(33 ): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'cs52::FlashDrive' (or there is no acceptable conversion) c:program filesmicrosoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,char)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(811): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,char)' with [ _E lem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(937): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(944): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,signed char)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(951): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(958): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0v cincludeostream(968): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<,cs52::FlashDrive>(std::basic_ostream<_Elem,_Traits> &&,_Ty)' with [ _Elem=char, _Traits=std::char_traits, _Ty=cs52::FlashDrive ] c:program filesmicrosoft visual studio 10.0vcincludeostream(1085): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(186): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,Traits> &(_cdecl *)(std::basic_ostream<_Elem,_Traits> &))' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(192): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,Traits> &(_cdecl *)(std::basic_ios<_Elem,_Traits> &))' with [ _Elem=char, _Traits=std::ch ar_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(199): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(206): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(226): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(260): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(280): or 'std::basic_ostrea m<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(305): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(325): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(345): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(366): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,Traits>::operator <<(_int64)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosof t visual studio 10.0vcincludeostream(386): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(407): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(427): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(447): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(467): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)' with [ _Elem=char, _Traits=std::char_traits ] c:program filesmicrosoft visual studio 10.0vcincludeostream(487): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)' with [ _Elem=char, _Traits=std::char_traits ] while trying to match the argument list '(std::ostream, cs52::FlashDrive)' c:documents and settingsadministratormy documentsvisual studio 2010projectsflashdriverflashdrivermain.cpp(38): error C2146: syntax error : missing ';' before identifier '–' c:documents and settingsadministratormy documentsvisual studio 2010projectsflashdriverflashdrivermain.cpp(38): error C2065: '–' : undeclared identifier c:documents and settingsadministratormy documentsvisual studio 2010projectsflashdriverflashdrivermain.cpp(38): error C2146: syntax error : missing ';' before identifier 'drive1' FlashDrive.cpp Generating Code...

Build FAILED.

Time Elapsed 00:00:01.82 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


You need to define operator>> for class FlashDrive

friend std::istream& operator>>(std::istream& is, FlashDrive& fd)
{
  is >> my_StorageCapacity >> my_StorageUsed >> my_IsPluggedIn;
  return is;
}

Similar you could define operator<<

std::ostream& operator>>(std::ostream& os, const FlashDrive& fd)
{
  os << fd.getCapacity() << fd.getUsed() << fd.isPluggedIn();
  return os;
}
链接地址: http://www.djcxy.com/p/68430.html

上一篇: 如何获得Unicode字符的字形unicode表示

下一篇: 类异常和操作符