我在定义命名空间时遇到了一些问题
据我所知,我做的一切都是对的
下面是我的代码和构建输出
闪存盘.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:32:24。InitializeBuildStatus:触摸“Debug\FlashDriver.unsuccessfulbuild”。ClCompile: Main.cpp c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(25): error C2065: 'cin' : undeclared identifier c:\documents and settings\管理员\我的文档\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(30): 错误 C2065: 'cout': 未声明的标识符 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver \flashdriver\main.cpp(30):错误 C2065:'endl':未声明的标识符 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(33): error C2065: 'cout' : undeclared identifier c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(33): 错误 C2065: 'endl': 未声明的标识符 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp( 35): 错误 C2146: 语法错误: 缺少';' 在标识符“-”之前 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(35): error C2065: '-' : undeclared identifier c:\documents and settings\管理员\我的文档\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(35):错误 C2146:语法错误:缺少“;” 在标识符“drive1”之前 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(36): error C2065: 'cout': undeclared identifier c:\documents and settings\管理员\我的文档\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(36): error C2065: 'endl' : undeclared identifier c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver \flashdriver\main.cpp(39): 错误 C2065: 'cout': 未声明的标识符 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(39): 错误 C2065 : 'endl' : 未声明的标识符 c: 错误 C2065:'endl':未声明的标识符 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(62):错误 C2146:语法错误:缺少 ';' 在标识符“-”之前 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(62): error C2065: '-' : undeclared identifier c:\documents and settings\管理员\我的文档\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(62):错误 C2146:语法错误:缺少 ';' 在标识符“组合”之前 c:\documents and settings\administrator\my documents\visual studio 2010\projects\flashdriver\flashdriver\main.cpp(63): error C2065: 'cout' : undeclared identifier c:
构建失败。
已用时间 00:00:02.50 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========