我在定义名称空间时遇到了一些麻烦
据我所知,我做对了所有事情
下面是我的代码和构建输出
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 ------构建开始于2013年7月29日4:32:24 AM。 InitializeBuildStatus:触摸“ Debug \ FlashDriver.unsuccessfulbuild”。 ClCompile:Main.cpp
c:\ documents and settings \ administrator \ mydocuments \ visual studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(25):错误C2065:“ cin”
:未声明的标识符c:\ documents and settings \ administrator \ my
文件\视觉工作室
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(30):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(30):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(33):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(33):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(35):错误C2146:
语法错误:缺少';'在标识符“ –”之前c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(35):错误C2065:“ –”:
未声明的标识符c:\ documents and settings \ administrator \ my
文件\视觉工作室
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(35):错误C2146:
语法错误:缺少';'在标识符'drive1'之前c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(36):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(36):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(39):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(39):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(42):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(42):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(46):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(46):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(49):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(49):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(53):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(53):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(56):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(56):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(62):错误C2146:
语法错误:缺少';'在标识符“ –” c:\ documents和之前
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(62):错误C2065:“ –”:
未声明的标识符c:\ documents and settings \ administrator \ my
文件\视觉工作室
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(62):错误C2146:
语法错误:缺少';'在标识符“组合” c:\ documents之前
和设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(63):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(63):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(72):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(72):错误C2065:
'endl':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(81):错误C2065:
'cout':未声明的标识符c:\ documents和
设置\管理员\我的文档\ Visual Studio
2010 \ projects \ flashdriver \ flashdriver \ main.cpp(81):错误C2065:
'endl':未声明的标识符FlashDrive.cpp生成代码...
建立失败。
经过的时间00:00:02.50
==========构建:0成功,1失败,0最新,跳过0 ==========
最佳答案
cout/cin/endl
来自标准名称空间,您需要使用标准名称空间对其进行限定
std::cout
std::cin
std::endl