我在定义命名空间时遇到了一些麻烦
据我所知,我做的一切都是正确的
以下是我的代码和构建输出
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...
}
}
这是构建错误 大部分都是多余的
------ Build build:Project:FlashDriver,Configuration:Debug Win32 ------ Build build 7/29/2013 4:32:24 AM。 InitializeBuildStatus:触摸&#34; Debug \ FlashDriver.unsuccessfulbuild&#34;。 ClCompile:Main.cpp c:\ documents and settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(25):错误C2065:&#39; cin&#39; :未声明的标识符c:\ documents和settings \ administrator \ my 文件\视觉工作室 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(30):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(30):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(33):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(33):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(35):错误C2146: 语法错误:缺少&#39;;&#39;在标识符&#39; - &#39;之前c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(35):错误C2065:&#39; - &#39; : 未声明的标识符c:\ documents和settings \ administrator \ my 文件\视觉工作室 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(35):错误C2146: 语法错误:缺少&#39;;&#39;在标识符&#39; drive1&#39;之前c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(36):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(36):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(39):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(39):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(42):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(42):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(46):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(46):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(49):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(49):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(53):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(53):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(56):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(56):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(62):错误C2146: 语法错误:缺少&#39;;&#39;在标识符&#39; - &#39;之前c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(62):错误C2065:&#39; - &#39; : 未声明的标识符c:\ documents和settings \ administrator \ my 文件\视觉工作室 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(62):错误C2146: 语法错误:缺少&#39;;&#39;在标识符&#39;组合之前C:\文件 和settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(63):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(63):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(72):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(72):错误C2065: &#39; ENDL&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(81):错误C2065: &#39; COUT&#39; :未声明的标识符c:\ documents和 settings \ administrator \ my documents \ visual studio 2010 \ projects \ flashdriver \ flashdriver \ main.cpp(81):错误C2065: &#39; ENDL&#39; :未声明的标识符FlashDrive.cpp生成代码...
构建失败。
时间流逝00:00:02.50 ==========构建:0成功,1个失败,0个最新,0个跳过==========
答案 0 :(得分:6)
cout/cin/endl
来自 std 名称空间,您需要使用 std 命名空间
std::cout
std::cin
std::endl