Overloading Pointers

As a part of our exam review package for next week, we are asked to understand pointers in overloaded operators-- something I am finding it very difficult to get and the instructor is refusing to give us any example, and asking his for independent study.

Apparently we will be having one of these problems on the final and I want to be sure I properly understand this practice exercise.

This practice concerns the issue of operator overloading. And we are asked to implement << and >>. And we are to :


Using the FlashDrive class you have been working with, upgrade the class so that
 operator << and operator >> work well with pointers (that is, FlashDrive *). Youll 
need to re-overload these operators, adding the function:

friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive );

friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive );

HINT: Be very careful to test for NULL

NOTE: These ideas will be a part on next weeks assignment!


I am having a lot of trouble getting
friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive );

friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive );

to work...

Please help!

My current code is

FlashDrive.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H
namespace cs52
{
class FlashDrive {

friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2);
friend FlashDrive operator- (FlashDrive used3, FlashDrive used4 );
friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive );
friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive );

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; 
int my_StorageUsed; 
bool my_IsPluggedIn;

    }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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cstdlib>
#include <iostream>
#include "FlashDrive.h"
using namespace cs52;
using namespace std;

std::ostream& operator <<(std::ostream& outs, const FlashDrive * drive )
{

   return outs;
}

std::istream& operator >>( std::istream& ins, FlashDrive * & drive )
{
   return ins;
}

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include "FlashDrive.h"
using namespace cs52;

void main( )
{

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

// work with the new stuff added for Unit 16!!!

cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;

drive3 = &drive2;
cout << drive3 << endl;

drive3 = new FlashDrive();
cin  >> drive3;
cout << drive3 << endl;

delete( drive3 );

}
Last edited on
There's really nothing particular about that:
1
2
3
4
5
6
7
8
9
10
11
12
std::ostream& operator <<(std::ostream& outs, const FlashDrive * drive )
{
   outs << drive->my_StorageCapacity << ' ' << ... ; // all other variables / Note the ' '
   return outs;
}

std::istream& operator >>( std::istream& ins, FlashDrive * & drive )
{
   drive = new FlashDrive();
   ins >> drive->my_StorageCapacity >> ...; // all other variables
   return ins;
}
Last edited on
What are the ...; in reference to?
And can I ask, why are there errors on

the '>>' in cin >> sample
and the '<<' in cout << sample

as expressed bewlow
1
2
3
4
5
6
7
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;

What are the ...; in reference to?
the other variables that you want to store like

outs << drive->my_StorageCapacity << ' ' << drive->my_StorageUsed; // << ... etc

ins >> drive->my_StorageCapacity >> drive->my_StorageUsed;

it's up do you to decide which variables. Don't forget the ' ' between each variable
Last edited on
Thanks,
I am still getting a significant errors code :-/


http://pastebin.com/tNJTZYmK
first: you need to put the operators within the namespace cs52

second: you made the operator >> and << for pointer. sample is not a pointer

So I would format it as

1
2
3
cs52::FlashDrive *sample;
cin >> sample;

?


And as far as I can tell the operators are within the namespace cs52.... do you mean change std::operator to cs52::operator?
Also, These error codes are really confusing me,

I know they sound self explanatory, but I can't seem to fix them with out causing errors on other parts of the code


------ Build started: Project: Pointer FlashDrive, Configuration: Debug Win32 ------
Build started 8/5/2013 3:18:50 AM.
InitializeBuildStatus:
  Touching "Debug\Pointer FlashDrive.unsuccessfulbuild".
ClCompile:
  FlashDriver.cpp
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer 
flashdrive\flashdriver.cpp(107): fatal error C1075: end of file found before the left 
brace '{' at '\\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(4)' was matched
  FlashDrive.cpp
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer 
flashdrive\flashdrive.cpp(63): fatal error C1075: end of file found before the left 
brace '{' at '\\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(4)' was matched
  Generating Code...

Build FAILED.

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




and doing what I said above fixed all but these two errors
Last edited on
So I would format it as
yes.

it looks like in FlashDrive.h you forgot to close } the namespace cs52, therefore everything is within the namespace.

that using namespace may work too, but I wouldn't recommend it.
The only problem I get is if I close the namespace after #endif I get the following error

------ Build started: Project: Pointer FlashDrive, Configuration: Debug Win32 ------
Build started 8/5/2013 3:27:20 AM.
InitializeBuildStatus:
  Touching "Debug\Pointer FlashDrive.unsuccessfulbuild".
ClCompile:
  FlashDrive.cpp
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer 
flashdrive\flashdrive.cpp(9): error C2248: 'cs52::FlashDrive::my_StorageCapacity' 
: cannot access private member declared in class 'cs52::FlashDrive'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(39) : see declaration of 
'cs52::FlashDrive::my_StorageCapacity'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(7) : see declaration of 'cs52::FlashDrive'
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer 
flashdrive\flashdrive.cpp(16): error C2248: 
'cs52::FlashDrive::my_StorageCapacity' : cannot access private member declared 
in class 'cs52::FlashDrive'
          \\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer flashdrive\flashdrive.h(39) : see declaration of 
'cs52::FlashDrive::my_StorageCapacity'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(7) : see declaration of 'cs52::FlashDrive'
  Generating Code...
  Compiling...
  FlashDriver.cpp
  Generating Code...

Build FAILED.

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



and if I close it before #endif I get this error


------ Build started: Project: Pointer FlashDrive, Configuration: Debug Win32 ------
Build started 8/5/2013 3:28:08 AM.
InitializeBuildStatus:
  Touching "Debug\Pointer FlashDrive.unsuccessfulbuild".
ClCompile:
  FlashDrive.cpp
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer 
flashdrive\flashdrive.cpp(9): error C2248: 'cs52::FlashDrive::my_StorageCapacity' 
: cannot access private member declared in class 'cs52::FlashDrive'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(39) : see declaration of 
'cs52::FlashDrive::my_StorageCapacity'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(7) : see declaration of 'cs52::FlashDrive'
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer 
flashdrive\flashdrive.cpp(16): error C2248: 
'cs52::FlashDrive::my_StorageCapacity' : cannot access private member declared 
in class 'cs52::FlashDrive'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(39) : see declaration of 
'cs52::FlashDrive::my_StorageCapacity'
          \\psf\home\documents\visual studio 2010\projects\pointer 
flashdrive\pointer flashdrive\flashdrive.h(7) : see declaration of 'cs52::FlashDrive'
  Generating Code...
  Compiling...
  FlashDriver.cpp
  Generating Code...

Build FAILED.

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




Where/How Should I close it so that these errors go away :-/
Last edited on
Just for reference here is my updated coding as of current

FlashDrive.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H

namespace cs52
{

class FlashDrive {

friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2);
friend FlashDrive operator- (FlashDrive used3, FlashDrive used4 );

public:
	friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive );
	friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive );
	FlashDrive& FlashDrive::operator=(int);
	FlashDrive::FlashDrive(int);
	FlashDrive(const std::string &name): name_(name){
	}
    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; 
std::string   name_;
int my_StorageUsed; 
bool my_IsPluggedIn;

	}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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdlib>
#include <iostream>
#include "FlashDrive.h"
using namespace cs52;
using namespace std;

ostream& operator <<(std::ostream& outs, const FlashDrive * drive )
{
   outs << drive->my_StorageCapacity << ' ';
   return outs;
}

istream& operator >>( std::istream& ins, FlashDrive * & drive )
{
   drive = new FlashDrive();
   ins >> drive->my_StorageCapacity;
   return ins;
}
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 );
}


Driver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <cstdlib>
#include "FlashDrive.h"

using namespace std;

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

// work with the new stuff added for Unit 16!!!

cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;

drive3 = &drive2;
cout << drive3 << endl;

drive3 = new FlashDrive();
cin  >> drive3;
cout << drive3 << endl;

delete( drive3 );

}
Last edited on
put the } before #endif

the
cannot access private member
tells you that you need to embed the operators within the namespace:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace cs52
{
ostream& operator <<(std::ostream& outs, const FlashDrive * drive )
{
   outs << drive->my_StorageCapacity << ' ';
   return outs;
}

istream& operator >>( std::istream& ins, FlashDrive * & drive )
{
   drive = new FlashDrive();
   ins >> drive->my_StorageCapacity;
   return ins;
}
}


Btw: you need the ' ' only to separate two variables. At the end it makes no sense. The reason is that during reading the stream can detect were one variable ends and the other begins.
Topic archived. No new replies allowed.