0

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

赋值指令如下

此分配的目的是处理异常。您可能还记得,我为您提供了一个名为 FlashDrive 的示例类,如下图所示。您可以在此处获取 FlashDrive 类的源代码(.NET 或 .NET 2010) 我希望您增强此类,以便调用其方法或运算符可能会引发异常,而不仅仅是将错误消息打印到 cout。目前,我们最喜欢的异常类是 std::logic_error。您可以通过将字符串值传递给其构造函数来创建 logic_error。正式地,您还应该说#include 以开始使用 logic_error,但 Visual Studio(作为一个行为不端的孩子......)让您在没有它的情况下逃脱。一旦一切正常,驱动程序代码应该按照课堂上的描述运行。

尽管示例驱动程序代码可能无法针对所有这些情况进行编码,但我希望您在以下情况下抛出异常:

放入驱动器的东西超过了它可以安全容纳的东西(由于 writeData)

负数可能用作 my_StorageUsed 值(由于操作员 - 或错误值被发送到构造函数调用)

一个负数可能用作 my_StorageCapacity 值(由于操作员 - 或错误的值被发送到构造函数调用)所以小心地通过类的所有操作员和方法,确保在每种情况下都抛出 logic_error。

我还希望您让 operator << 和 operator >> 为 FlashDrive 类工作。最后,我希望您将 FlashDrive 放入命名空间 cs52。

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

闪存盘.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 

闪存盘.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 );
}
}

主文件

#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:触摸“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 '>>' : no operator found which take a left - 'std::istream' 类型的手动操作数(或没有可接受的转换) c:\program files\microsoft visual studio 10.0\vc\include\istream(1053): 可以是 'std::basic_istream<_Elem,_Traits > &std::运算符

>(std::basic_istream<_Elem,_Traits> &&,signed char *)' with [ _Elem=char, _Traits=std::char_traits ] 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): 或 '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): 或 'std::basic_istream<_Elem,_Traits> &std::operator >( std::basic_istream<_Elem,_Traits> &&,unsigned char &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(1097):或'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &,signed char *)' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(1104): 或 'std::basic_istream<_Elem,_Traits> &std::operator >(std::basic_istream<_Elem,_Traits> &,用 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(1111): 或 'std::basic_istream<_Elem,_Traits> &std 签名的 char &)' ::operator >(std::basic_istream<_Elem,_Traits> &,unsigned char *)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(1118): 或 '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(1128): 或 '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): 或'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 files\microsoft visual studio 10.0\vc\include\istream (185): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator (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):或 '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): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem ,_Traits>::operator >>(short &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(253): 或 'std: :basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(272): 或 '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 &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(316):或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(334): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)' with [ _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> &std::basic_istream<_Elem,_Traits>::operator >>(long &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream (334): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)' with [ _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> &std::basic_istream<_Elem,_Traits>::operator >>(long &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream (334): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)' with [ _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,>(long &)' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(334): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(第354章:>(long &)' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(334): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(第354章:char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(334): 或 '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,char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(334): 或 '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,使用 [ _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 ,使用 [ _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 ,特性>::操作符>>(_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, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(392) : 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\istream(411): 或 '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): 或 '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):或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\ microsoft visual studio 10.0\vc\include\istream(466): 或 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator (std::basic_streambuf<_Elem,_Traits> *)'使用 [ _Elem=char, _Traits=std::char_traits ] 尝试匹配参数列表 '(std::istream, cs52::FlashDrive)' c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(33): error C2679: binary '<<' : no operator found which take a right-hand operation of type 'cs52 ::FlashDrive'(或没有可接受的转换) c:\program files\microsoft visual studio 10.0\vc\include\ostream(679): 可能是 'std::basic_ostream<_Elem,_Traits> &std::operator << >(std::basic_ostream<_Elem,_Traits> &,const char *)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(726) : 或 'std::basic_ostream<_Elem,_Traits> &std::运算符 <<>(std::basic_ostream<_Elem,_Traits> &,char)' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(764 ): 或 '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): 或 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,字符)'使用 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(937): 或 'std::basic_ostream<_Elem,_Traits> &std::operator << >(std::basic_ostream<_Elem,_Traits> &,const signed char *)' with [ _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:\program files\microsoft visual studio 10.0\vc\include\ostream(951): 或 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream <_Elem,_Traits> &,const unsigned char *)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(958): 或 'std: :basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(968): 或 '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): 或 '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 files\microsoft visual studio 10.0\vc\include\ostream(186): 或 '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 files\microsoft visual studio 10.0\vc\include\ostream (192): 或 'std::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::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(199):或 '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): 或 '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): 或 '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): 或 'std:: basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(280): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int) ' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(305): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream< _Elem,_Traits>::operator <<(unsigned int)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(325): 或 '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\包括\ostream(345): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Elem,_Traits>::operator <<(long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): 或 'std: :basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\ vc\include\ostream(366): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Elem,_Traits>::operator <<(long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): 或 'std: :basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\ vc\include\ostream(366): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): 或 'std::basic_ostream<_Elem,_Traits > &std::basic_ostream<_Elem,_Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): 或 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' with [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): 或 'std::basic_ostream<_Elem,_Traits > &std::basic_ostream<_Elem,运算符 <<(unsigned long)' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): 或 'std::basic_ostream<_Elem, _Traits> &std::basic_ostream<_Elem,运算符 <<(unsigned long)' 与 [ _Elem=char, _Traits=std::char_traits ] c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): 或 'std::basic_ostream<_Elem, _Traits> &std::basic_ostream<_Elem,特征>::操作符<<(\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38): error C2146: syntax error: missing ';' 在标识符“-”之前 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38): error C2065: '-' : undeclared identifier c:\documents and settings\管理员\我的文档\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38):错误 C2146:语法错误:缺少 ';' 在标识符 'drive1' FlashDrive.cpp 生成代码之前... \documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38): error C2065: '-' : undeclared identifier c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38):错误 C2146:语法错误:缺少 ';' 在标识符 'drive1' FlashDrive.cpp 生成代码之前... \documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38): error C2065: '-' : undeclared identifier c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(38):错误 C2146:语法错误:缺少 ';' 在标识符 'drive1' FlashDrive.cpp 生成代码之前...

构建失败。

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

4

1 回答 1

1

你需要operator>>定义class FlashDrive

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;
}
于 2013-07-29T12:10:42.810 回答