Operator Overloading problem

Hi,
I am having some trouble with overloading the "-" operator.

I am just going to paste the code here because everytime I try to explain the problem to the people, I can't make it clear and they get mad.

Here is the code I am having trouble with;


1
2
3
4
5
6
7
FlashDrive operator+ (FlashDrive used1, FlashDrive used2 ) {
	
	FlashDrive plus;

	plus.my_StorageUsed = (used1.getUsed()+ used2.getUsed());
	return plus;			
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FlashDrive operator - (FlashDrive used1, FlashDrive used2 ){
	FlashDrive minus;
	
	if(used1.getUsed()<0 || used2.getUsed()<0) {
		cout <<"One of the variables is negative or not initialized";
		throw logic_error("Negative or uninitialized varibale");
	}
	else {

	minus.my_StorageUsed = ((used1.getUsed()+ used2.getUsed())-used1.getUsed());
	
	if(minus.my_StorageUsed > 0) {
	return minus;
	}
	else {
		cout<<"Result is a negative number"<< endl;
		throw logic_error("Negative result");
	
	}
	}
}


1
2
3
4
 inline FlashDrive& operator = (const FlashDrive& usedtotal){
		my_StorageUsed= usedtotal.my_StorageUsed;
		return *this; 
	}



1
2
FlashDrive combined = drive1 + drive2 ;
cout << "this drive's filled to " << combined.getUsed( ) << endl;


This is the problem I am having, VS underlines "-" and says undcleared identifier.
What part of my code causes this problem?
I am suppose to throw a logic_error since empty holds no value and it's uninitialized.
But I can't get the operators work for this.

1
2
3
4
5
6
7
8
try {
  FlashDrive 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...
}


Please let me know if my question is not clear enough, I will try to explain it more.
It would help if you posted the actual compiler error with an indication of the line. The compiler is telling you what's wrong. So far you've omitted a description of the error.

As an aside, you should be passing those FlashDrive objects by const reference rather than by value.
Error 1 error C2146: syntax error : missing ';' before identifier '–' c:\users\cavıdan\desktop\odev2\flashdrive\main.cpp 94

Error 3 error C2146: syntax error : missing ';' before identifier 'combined' c:\users\cavıdan\desktop\odev2\flashdrive\main.cpp 94

Error 4 error C2065: 'combined' : undeclared identifier c:\users\cavıdan\desktop\odev2\flashdrive\main.cpp 94

5 IntelliSense: expected a ';' c:\users\cavıdan\desktop\odev2\flashdrive\main.cpp 94

1
2
3
4
5
6
7
8
9
try {
  FlashDrive empty = empty – combined; // this is the line 94
  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...
}


As an aside, you should be passing those FlashDrive objects by const reference rather than by value.


Do you mean I should do like this?
FlashDrive operator - (const FlashDrive& used1,const FlashDrive& used2 )
FlashDrive operator - (const FlashDrive& used1,const FlashDrive& used2 )

try remove the space between operator and -, this way:

FlashDrive operator- (const FlashDrive& used1,const FlashDrive& used2 )]
Last edited on
I changed them to this as you suggested.

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
FlashDrive operator+ (const FlashDrive& used1, const FlashDrive& used2) {
	
	FlashDrive plus;

	plus.my_StorageUsed = (used1.getUsed()+ used2.getUsed());
	return plus;												
}

FlashDrive operator- (const FlashDrive& used1, const FlashDrive& used2){
	FlashDrive minus;
	
	if(used1.getUsed()<0 || used2.getUsed()<0) {
		cout <<"One of the variables is negative or not initialized";
		throw logic_error("Negative or uninitialized varibale");
	}
	else {

	minus.my_StorageUsed = ((used1.getUsed()+ used2.getUsed())-used1.getUsed());
	
	if(minus.my_StorageUsed > 0) {
	return minus;
	}
	else {
		cout<<"Result is a negative number"<< endl;
		throw logic_error("Negative result");
	
	}
	}
}


FlashDrive.h
1
2
friend FlashDrive operator+ (const FlashDrive& used1, const FlashDrive& used2);
friend FlashDrive operator- (const FlashDrive& used1, const FlashDrive& used2);


And also I had to change the member function for that which I did like this,
1
2
3
int  FlashDrive::getUsed( ) const {
  return( my_StorageUsed );
}


But I still have the same errors.
i dont have access to a compiler right now, i think problem mite be with the way u are overloading, u need not pass both the variables as parameters, u can directly access the members of 1st variable, just pass 2nd variable as parameter:

ex:
1
2
3
4
5
6
7
FlashDrive operator- ( const FlashDrive& used2){
	FlashDrive minus;
	
	if(getUsed()<0 || used2.getUsed()<0) {
		cout <<"One of the variables is negative or not initialized";
		throw logic_error("Negative or uninitialized varibale");
	}

just give it a try, make corrections everywhere similar to that, even in +operator, im not sure if thats the cause.
FlashDrive empty = empty – combined;

the empty after the equals sign is undefined.
closed account (Dy7SLyTq)
@op: it would help to post all of your code, because we have no idea what is on those lines.

@anir: its the same thing. yours is the operator belongs to the class, his is outside of it. it doesnt make a difference i dont think and not really important right now

This is a strange line

FlashDrive empty = empty – combined; // this is the line 94

you use empty before it's constructed (the constructor is called).

I tested it with GCC:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class C { };

C operator - (C x, C y)
{
  return C();
}

int main()
{

  C a;
  C z = z - a; // consider to overload -= instead

  return 0;
}
it seems not to be a problem.

It seems like your compiler has a problem with empty
closed account (Dy7SLyTq)
does
FlashDrive empty = empty – combined; // this is the line 94

call the default or constructor (or empty constructor if defined) at the empty before the = sign? because if it does, he could have defined a constructor for it that takes no args, thus making it defined instead of undefined
Here is my full code.

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
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H
#include <string>
#include <iostream>

//some functions are inline so I would not get multiple definitions of functions
// creating namespace
namespace cs52 {

class FlashDrive {
	
	

public:
	//Using friend to access private data such as my_StorageUsed
	friend FlashDrive operator+ (const FlashDrive& used1, const FlashDrive& used2);
	friend FlashDrive operator- (const FlashDrive& used1, const FlashDrive& used2);
	friend bool operator< (FlashDrive &lhs,FlashDrive &rhs );
	friend bool operator> (FlashDrive &lhs,FlashDrive &rhs );
	friend std::istream& operator >> (std::istream& ins, FlashDrive* & drive);  //overloading the input operator
	friend std::ostream& operator << (std::ostream& outs, const FlashDrive* drive); //overloading the output operator
	
	inline FlashDrive& FlashDrive::operator=(int);
	inline FlashDrive::FlashDrive(int){} // to convert int to FlashDrive type
	
	//Overloading assignment operator to assign combined and other 
    inline FlashDrive& operator = (const FlashDrive& usedtotal){
		my_StorageUsed= usedtotal.my_StorageUsed;
		return *this; 
	}

    FlashDrive( );
	FlashDrive( int capacity, int used, bool pluggedIn );
	~FlashDrive(){}//deconstructor

	void plugIn( );
	void pullOut( );
	void writeData( int amount );
	void eraseData( int amount );
	void formatDrive( );

	int  getCapacity( );
	void setCapacity( int amount );
	int  getUsed( ) const;
	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?
};

}

 #endif  

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "FlashDrive.h"
#include <stdexcept>
#include <iostream>
using namespace std;
//Source file for the class




namespace cs52 {


FlashDrive::FlashDrive( ) {
  my_StorageCapacity = 0;
  my_StorageUsed = 0;
  my_IsPluggedIn = false;
  
}

FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn ) { 
	if(capacity<0 && used<0) {
		throw logic_error("Negative input");
	}
	else {

	  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 ) {
	if(amount>my_StorageCapacity) {
		throw logic_error("Not enough capacity");
	}
	else{
	 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( ) const {
  return( my_StorageUsed );
}

void FlashDrive::setUsed( int amount ) {
  my_StorageUsed = amount;
}

bool FlashDrive::isPluggedIn( ) {
  return( my_IsPluggedIn );
}

//--------------------------------------------------------------------


//Overloading the + operator to get combined
FlashDrive operator+ (const FlashDrive& used1, const FlashDrive& used2) {
	
	FlashDrive plus;

	plus.my_StorageUsed = (used1.getUsed()+ used2.getUsed());// gets the used data for drive1 and drive2 and puts it inside 
	return plus;												//plus which will be assigned to the combined 
}


//Overloading the bool <,> operators for comparisons
bool operator< (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( lhs.getUsed() < rhs.getUsed() );
}


bool operator> (FlashDrive &lhs,FlashDrive &rhs ) {
   return ( operator <( rhs, lhs ) );
}


//Overloadging  - to get other 
FlashDrive operator- (const FlashDrive& used1, const FlashDrive& used2){
	FlashDrive minus;
	
	if(used1.getUsed()<0 || used2.getUsed()<0) {
		cout <<"One of the variables is negative or not initialized";
		throw logic_error("Negative or uninitialized varibale");
	}
	else {

	minus.my_StorageUsed = ((used1.getUsed()+ used2.getUsed())-used1.getUsed());
	
	if(minus.my_StorageUsed > 0) {
	return minus;
	}
	else {
		cout<<"Result is a negative number"<< endl;
		throw logic_error("Negative result");
	
	}
	}
}

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

}
std::ostream& operator << (std::ostream& outs, const FlashDrive* drive) {
	
	if(drive==NULL) {
		cout <<"\nNULL Pointer";	//checking for NULL pointer
	}
	else {

	outs << drive->my_StorageCapacity <<" "<< drive->my_StorageUsed<<endl;
	}
	return (outs);


}
}


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include "FlashDrive.h"// header file for the class
#include <stdexcept>

using namespace std;
using namespace cs52;



void main( )
{
 
	int cap1=0,used1=0,cap2=0,used2=0;
	
	
	
cout << "Welcome to Howie's Flash USB Drive Store!\n" //added this from the previous flashdrive provided
      << endl;
//Getting input from user
cout<< "For drive 1 please enter total capacity(>0,and number only),\nand storage used(number only): ";
cin>> cap1 >> used1;
cout<<"\n"<<cap1 << " "<< used1<<"\n";

cout<< "\nFor drive 2 please enter total capacity(>0,and number only),\nand storage used(number only): ";
cin>> cap2 >> used2;
cout<<"\n"<<cap2 <<" "<< used2<<"\n";


//Definition and initialization of drive1 and drive2
FlashDrive drive1( cap1, used1, false );//get the parameters for capacity,initial used data, and initial pluggedin check
FlashDrive drive2( cap2, used2, false );

//FlashDrive empty(0, 0, false);


cout<<"\nCapacity(1-2): "<< drive1.getCapacity()<<" "<<drive2.getCapacity() <<" Used Storage(1-2): "<< drive1.getUsed()<<" "<< drive2.getUsed()<<endl;


//Calling the functions for drive1 and drive2
//write data is added to the my_StorageUsed
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData(used1);//user input as param
drive1.pullOut( );

drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData(used2);//user input as param
drive2.pullOut( );

try {
//Defined combined and used it to hold drive1 and drive2 using overloaded "+" and "=" operators
FlashDrive combined = drive1 + drive2 ;
cout << "this drive's filled to " << combined.getUsed( ) << endl;

//Defined other and used it to hold (drive1+drive2)-drive1 by using overloaded "-" and "=" functions to get 
FlashDrive other = combined - drive1;
cout << "the other cup's filled to " << other.getUsed( ) << endl;


//checking for the conditions using overloaded bool ">" and "<" argument
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;
}

} catch (logic_error) {
	cout<<"caught logic_error"<<endl;
	cout<<"Cannot check the conditions"<<endl;
}


// let's throw some exceptions...

try {
  FlashDrive 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 ) {
	cout << "\nNot enough capacity for this..\n";

}

try {
  cs52::FlashDrive f( -1, -1, false );
  cout << "something not right here..." << endl;
} catch( std::logic_error ) {
	cout<<"\nNegative input\n";
}

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

cs52::FlashDrive * drive3=NULL;
// careful...
cout << drive3 << endl;		//Prints "NULL Pointer" warning


drive3 = &drive2;		//puts drive2 capacity and used into drive3
cout <<"\n"<< drive3 <<"^^^ drive3=&drive2 "<< endl;

drive3 = new FlashDrive();
cout <<"\nInput for drive3 capacity and used: ";
cin  >> drive3;											//user input for drive3 capacity and used
cout <<"\n"<< drive3 <<"^^^ drive3 after input "<< endl;

delete( drive3 );


}


Strictly speaking, the statement FlashDrive empty = empty – combined; allocates memory for one FlashDrive on stack and passes a reference to this uninitialized storage to operator- as the left-hand argument. The moment operator- tries to do something that isn't permitted for such references (not much is), such as calling used1.getUsed(), undefined behavior takes over.

I do not get it.
FlashDrive empty = empty – combined; how come this is uninitialized and compiler does not even build the code and this one works perfectly fine FlashDrive combined = drive1 + drive2 ;

I thought constructer would handle this empty variable for me?
How can I make it work if constructor is not working?
Last edited on
Oh, and regarding the original error:
Error 1 error C2146: syntax error : missing ';' before identifier '–' c:\users\cavıdan\desktop\odev2\flashdrive\main.cpp 94

'–' is NOT '-'
OMG, I it was all because a typo.
I am going to go ahead and kill myself.


Now it says "combined" is not defined on the same line...
However it is defined.
Is it because how I pass agrugments to the overloaded functions?
Now it says "combined" is not defined on the same line...
However it is defined.
It is defined on line 53, in scope of a try block, which ended at line 83. Past that line, `combined` does not exist
Thank you guys sooo much.
It is working properly finally.
Topic archived. No new replies allowed.