Help with OOP

Pages: 12
this is my .cpp file:
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "english_weights.h"

//Set EnglishWeight to 0.0.
EnglishWeight::EnglishWeight() {
pounds = 0;
ounces = 0;
}

//Set EnglishWeight to a specified value.
EnglishWeight::EnglishWeight(int lbs, int ozs) {
pounds = lbs;
ounces = ozs;
}

int EnglishWeight::getPounds(){
return this->pounds;
}

int EnglishWeight::getOunces(){
return this->ounces;
}

EnglishWeight operator - (EnglishWeight &subtract_ew) const {
int p1 = this->pounds - subtract_ew.pounds;
int p2 = this->ounces - subtract_ew.ounces;
return EnglishWeight(p1, p2);
}

EnglishWeight operator / (EnglishWeight &divide_ew) const {
if(divide_ew = 0)
false;
else {
int p1 = this.pounds / divide_ew.pounds;
int p2 = this.pounds / divide_ew.ounces;
return EnglishWeight(p1, p2);
}
}

EnglishWeight operator * (const EnglishWeight& mult_ew, const int number) {
int p1 = mult_ew.pounds * number;
int p2 = mult_ew.ounces * number;
return EnglishWeight(p1, p2);
}

EnglishWeight operator * (const int number, const EnglishWeight& mult_ew) {
int p1 = number * mult_ew.pounds;
int p2 = number * mult_ew.ounces;
return EnglishWeight(p1, p2);
}

EnglishWeight operator / (const EnglishWeight& divide_ew, const int number) {
if (number = 0)
false;
else{
int p1 = divide_ew.pounds / number;
int p2 = divide_ew.ounces / number;
return EnglishWeight(p1, p2);
}
}

// Define stream insertion operator for EnglishWeight.
ostream& operator << (ostream& str, EnglishWeight& ew_out) {
str << ew_out.pounds << "lbs " << ew_out.ounces << "ozs";
return str;
}

// Define stream extraction operator for EnglishWeight.
istream& operator >> (istream& str, EnglishWeight& ew_in) {
str >> ew_in.pounds;
if (str.get() != '\'') {
cerr << "*** Error with pounds. Incorrect format ***\n\n";
exit(1);
}
str >> ew_in.ounces;
if (str.get() != '"') {
cerr << "*** Error with ounces. Incorrect format ***\n\n";
exit(1);
} return str;
}

EnglishWeight operator + (EnglishWeight &add_ew) const {
int p1 = this.pounds + add_ew.pounds;
int p2 = this.ounces + add_ew.ounces;
return EnglishWeight(p1, p2);
}

this is my .h file:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;

class EnglishWeight {
private:
int pounds;
int ounces;

public:

//Constructor with no argument. Used to init EnglishWeight to 0.0.
EnglishWeight();

//Constructor to init weight to a specified value.
EnglishWeight(int lbs, int ozs);

int getPounds();
int getOunces();

//Computes EnglishWeight + EnglishWeight.
EnglishWeight operator + (EnglishWeight &add_ew) const;

//Computes EnglishWeight - EnglishWeight.
EnglishWeight operator - (EnglishWeight &subtract_ew) const;

//Computes EnglishWeight / EnglishWeight.
EnglishWeight operator / (EnglishWeight &divide_ew) const;

//Computes EnglishWeight * number.
friend EnglishWeight operator * (const EnglishWeight& mult_ew, const int number);

//Computes number * EnglishWeight.
friend EnglishWeight operator * (const int number, const EnglishWeight& mult_ew);

//Computes EnglishWeight / number.
friend EnglishWeight operator / (const EnglishWeight& divide_ew, const int number);

//Stream insertion operator for EnglishWeight.
friend ostream& operator << (ostream& str, EnglishWeight& ew_out);

//Stream extraction operator for EnglishWeight.
friend istream& operator >> (istream& str, EnglishWeight& ew_in);
};

Please help me, I don't know how to make this code work! I'm calling objects not in the right way and I cannot seem to get it to work.
Hey and welcome to this forum, please edit your post and use code tags - http://www.cplusplus.com/articles/jEywvCM9/

What is your problem, are you getting errors? if so, post them please.
I am getting the following errors:

[code]/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:33:55: error: non-member function cannot have 'const' qualifier
EnglishWeight operator - (EnglishWeight &subtract_ew) const {
                                                      ^~~~~~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:34:12: error: invalid use of 'this' outside of a non-static member function
  int p1 = this->pounds - subtract_ew.pounds;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:34:39: error: 'pounds' is a private member of 'EnglishWeight'
  int p1 = this->pounds - subtract_ew.pounds;
                                      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:14:9: note: declared private here
    int pounds;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:35:12: error: invalid use of 'this' outside of a non-static member function
  int p2 = this->ounces - subtract_ew.ounces;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:35:39: error: 'ounces' is a private member of 'EnglishWeight'
  int p2 = this->ounces - subtract_ew.ounces;
                                      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:15:9: note: declared private here
    int ounces;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:39:53: error: non-member function cannot have 'const' qualifier
EnglishWeight operator / (EnglishWeight &divide_ew) const {
                                                    ^~~~~~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:39:15: error: overloaded 'operator/' must be a binary operator (has 1 parameter)
EnglishWeight operator / (EnglishWeight &divide_ew) const {
              ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:40:16: error: no viable overloaded '='
  if(divide_ew = 0) 
     ~~~~~~~~~ ^ ~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:12:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'int' to 'const EnglishWeight' for 1st argument
class EnglishWeight {
      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:43:14: error: invalid use of 'this' outside of a non-static member function
    int p1 = this.pounds / divide_ew.pounds;
             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:43:38: error: 'pounds' is a private member of 'EnglishWeight'
    int p1 = this.pounds / divide_ew.pounds;
                                     ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:14:9: note: declared private here
    int pounds;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:44:14: error: invalid use of 'this' outside of a non-static member function
    int p2 = this.pounds / divide_ew.ounces;
             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:44:38: error: 'ounces' is a private member of 'EnglishWeight'
    int p2 = this.pounds / divide_ew.ounces;
                                     ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:15:9: note: declared private here
    int ounces;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:62:14: error: read-only variable is not assignable
  if (number = 0)
      ~~~~~~ ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:91:50: error: non-member function cannot have 'const' qualifier
EnglishWeight operator + (EnglishWeight &add_ew) const { 
                                                 ^~~~~~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:92:12: error: invalid use of 'this' outside of a non-static member function
  int p1 = this.pounds + add_ew.pounds;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:92:33: error: 'pounds' is a private member of 'EnglishWeight'
  int p1 = this.pounds + add_ew.pounds;
                                ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:14:9: note: declared private here
    int pounds;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:93:12: error: invalid use of 'this' outside of a non-static member function
  int p2 = this.ounces + add_ew.ounces;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:93:33: error: 'ounces' is a private member of 'EnglishWeight'
  int p2 = this.ounces + add_ew.ounces;
                                ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:15:9: note: declared private here
    int ounces;
        ^
18 errors generated.
[Finished in 0.3s with exit code 1][/code]
thank you so much for your help and sorry for not code tagging it!
this is my .cpp class:

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
 
#include <iostream>  
#include <stdlib.h>
#include <math.h>
#include "english_weights.h"   
 
//Set EnglishWeight to 0.0. 
EnglishWeight::EnglishWeight() {
  pounds = 0;
  ounces = 0;
}
     
//Set EnglishWeight to a specified value.
EnglishWeight::EnglishWeight(int lbs, int ozs) {
  pounds = lbs;
  ounces = ozs;
}

int EnglishWeight::getPounds(){
  return this->pounds;
}

int EnglishWeight::getOunces(){
  return this->ounces;
}

EnglishWeight operator - (EnglishWeight &subtract_ew) const {
  int p1 = this->pounds - subtract_ew.pounds;
  int p2 = this->ounces - subtract_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator / (EnglishWeight &divide_ew) const {
  if(divide_ew = 0) 
    false;  
  else {
    int p1 = this.pounds / divide_ew.pounds;
    int p2 = this.pounds / divide_ew.ounces;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight operator * (const EnglishWeight& mult_ew, const int number) {
  int p1 = mult_ew.pounds * number;
  int p2 = mult_ew.ounces * number;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator * (const int number, const EnglishWeight& mult_ew) {
  int p1 = number * mult_ew.pounds;
  int p2 = number * mult_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator / (const EnglishWeight& divide_ew, const int number) {
  if (number = 0)
    false;
  else{
    int p1 = divide_ew.pounds / number;
    int p2 = divide_ew.ounces / number;
    return EnglishWeight(p1, p2);
  }
}

// Define stream insertion operator for EnglishWeight.
ostream& operator << (ostream& str, EnglishWeight& ew_out) {
  str << ew_out.pounds << "lbs " << ew_out.ounces << "ozs";
  return str;
}
 
// Define stream extraction operator for EnglishWeight.
istream& operator >> (istream& str, EnglishWeight& ew_in) {
  str >> ew_in.pounds;
  if (str.get() != '\'') {
   cerr << "*** Error with pounds. Incorrect format ***\n\n";
   exit(1);
  }
  str >> ew_in.ounces;
  if (str.get() != '"') {
   cerr << "*** Error with ounces. Incorrect format ***\n\n";
   exit(1);
  } return str;
}

EnglishWeight operator + (EnglishWeight &add_ew) const { 
  int p1 = this.pounds + add_ew.pounds;
  int p2 = this.ounces + add_ew.ounces;
  return EnglishWeight(p1, p2);
}


This is my .h 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
#include <iostream>  
#include <stdlib.h>
#include <math.h>
using namespace std;

class EnglishWeight {
  private:
    int pounds;
    int ounces;
 
  public:          
    
    //Constructor with no argument. Used to init EnglishWeight to 0.0. 
    EnglishWeight();                   
    
    //Constructor to init weight to a specified value.
    EnglishWeight(int lbs, int ozs);  

    int getPounds();
    int getOunces();

    //Computes EnglishWeight + EnglishWeight.
    EnglishWeight operator + (EnglishWeight &add_ew) const;   
    
    //Computes EnglishWeight - EnglishWeight.
    EnglishWeight operator - (EnglishWeight &subtract_ew) const;   
 
    //Computes EnglishWeight / EnglishWeight.
    EnglishWeight operator / (EnglishWeight &divide_ew) const;   
 
    //Computes EnglishWeight * number.
    friend EnglishWeight operator * (const EnglishWeight& mult_ew, const int number);   
     
    //Computes number * EnglishWeight.
    friend EnglishWeight operator * (const int number, const EnglishWeight& mult_ew);   
 
    //Computes EnglishWeight / number.
    friend EnglishWeight operator / (const EnglishWeight& divide_ew, const int number);   
  
    //Stream insertion operator for EnglishWeight.
    friend ostream& operator << (ostream& str, EnglishWeight& ew_out);   
                                                             
    //Stream extraction operator for EnglishWeight.
    friend istream& operator >> (istream& str, EnglishWeight& ew_in); 
};
to start.

1
2
 int p1 = this.pounds + add_ew.pounds;
 int p2 = this.ounces + add_ew.ounces;


like your error is telling you, this is now how you use this. use -> instead of dots

1
2
 int p1 = this->pounds + add_ew.pounds;
  int p2 = this->ounces + add_ew.ounces;


Change all of them to ->.

Edit:

And try removing all the const for non-member functions such as this one -

 EnglishWeight operator / (EnglishWeight &divide_ew) const;   


remove the consts from both the h file and cpp file.
Last edited on
Hi thank you for your help! So I removed the const for all non member functions and 3 corresponding errors were reduced. But C++ still did not like my use of "this". Here is my new .cpp 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
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
#include <iostream>  
#include <stdlib.h>
#include <math.h>
#include "english_weights.h"   
 
//Set EnglishWeight to 0.0. 
EnglishWeight::EnglishWeight() {
  pounds = 0;
  ounces = 0;
}
     
//Set EnglishWeight to a specified value.
EnglishWeight::EnglishWeight(int lbs, int ozs) {
  pounds = lbs;
  ounces = ozs;
}

int EnglishWeight::getPounds(){
  return this->pounds;
}

int EnglishWeight::getOunces(){
  return this->ounces;
}

EnglishWeight operator - (EnglishWeight &subtract_ew)  {
  int p1 = this->pounds - subtract_ew.pounds;
  int p2 = this->ounces - subtract_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator / (EnglishWeight &divide_ew)  {
  if(divide_ew = 0) 
    false;  
  else {
    int p1 = this->pounds / divide_ew.pounds;
    int p2 = this->pounds / divide_ew.ounces;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight operator * (const EnglishWeight& mult_ew, const int number) {
  int p1 = mult_ew.pounds * number;
  int p2 = mult_ew.ounces * number;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator * (const int number, const EnglishWeight& mult_ew) {
  int p1 = number * mult_ew.pounds;
  int p2 = number * mult_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator / (const EnglishWeight& divide_ew, const int number) {
  if (number = 0)
    false;
  else{
    int p1 = divide_ew.pounds / number;
    int p2 = divide_ew.ounces / number;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight operator + (EnglishWeight &add_ew)  { 
  int p1 = this->pounds + add_ew.pounds;
  int p2 = this->ounces + add_ew.ounces;
  return EnglishWeight(p1, p2);
}

// Define stream insertion operator for EnglishWeight.
ostream& operator << (ostream& str, EnglishWeight& ew_out) {
  str << ew_out.pounds << "lbs " << ew_out.ounces << "ozs";
  return str;
}
 
// Define stream extraction operator for EnglishWeight.
istream& operator >> (istream& str, EnglishWeight& ew_in) {
  str >> ew_in.pounds;
  if (str.get() != '\'') {
   cerr << "*** Error with pounds. Incorrect format ***\n\n";
   exit(1);
  }
  str >> ew_in.ounces;
  if (str.get() != '"') {
   cerr << "*** Error with ounces. Incorrect format ***\n\n";
   exit(1);
  } return str;
}


here are the errors:

/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:34:12: error: invalid use of 'this' outside of a non-static member function
  int p1 = this->pounds - subtract_ew.pounds;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:34:39: error: 'pounds' is a private member of 'EnglishWeight'
  int p1 = this->pounds - subtract_ew.pounds;
                                      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:14:9: note: declared private here
    int pounds;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:35:12: error: invalid use of 'this' outside of a non-static member function
  int p2 = this->ounces - subtract_ew.ounces;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:35:39: error: 'ounces' is a private member of 'EnglishWeight'
  int p2 = this->ounces - subtract_ew.ounces;
                                      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:15:9: note: declared private here
    int ounces;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:39:15: error: overloaded 'operator/' must be a binary operator (has 1 parameter)
EnglishWeight operator / (EnglishWeight &divide_ew)  {
              ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:40:16: error: no viable overloaded '='
  if(divide_ew = 0) 
     ~~~~~~~~~ ^ ~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:12:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'int' to 'const EnglishWeight' for 1st argument
class EnglishWeight {
      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:43:14: error: invalid use of 'this' outside of a non-static member function
    int p1 = this->pounds / divide_ew.pounds;
             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:43:39: error: 'pounds' is a private member of 'EnglishWeight'
    int p1 = this->pounds / divide_ew.pounds;
                                      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:14:9: note: declared private here
    int pounds;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:44:14: error: invalid use of 'this' outside of a non-static member function
    int p2 = this->pounds / divide_ew.ounces;
             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:44:39: error: 'ounces' is a private member of 'EnglishWeight'
    int p2 = this->pounds / divide_ew.ounces;
                                      ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:15:9: note: declared private here
    int ounces;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:62:14: error: read-only variable is not assignable
  if (number = 0)
      ~~~~~~ ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:72:12: error: invalid use of 'this' outside of a non-static member function
  int p1 = this->pounds + add_ew.pounds;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:72:34: error: 'pounds' is a private member of 'EnglishWeight'
  int p1 = this->pounds + add_ew.pounds;
                                 ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:14:9: note: declared private here
    int pounds;
        ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:73:12: error: invalid use of 'this' outside of a non-static member function
  int p2 = this->ounces + add_ew.ounces;
           ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:73:34: error: 'ounces' is a private member of 'EnglishWeight'
  int p2 = this->ounces + add_ew.ounces;
                                 ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.h:15:9: note: declared private here
    int ounces;
        ^
15 errors generated.
[Finished in 0.5s with exit code 1]
oh yea, I see your problem now, you're definitios are written wrong. Non of your functions are written as if they're part of the class.

EnglishWeight operator - (EnglishWeight &subtract_ew)

It should be :
EnglishWeight EnglishWeight::operator-(EnglishWeight& subtract_ew)

Make this change for all of them.

And you also have a few if statements that look like this if (number = 0)

= is an assignment operator. You want == which is equal to operator.
Last edited on
OMG! I am always so careful to remember to write it this way!! I just added those and i have only 4 errors remaining!
.ccp class:
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
#include <iostream>  
#include <stdlib.h>
#include <math.h>
#include "english_weights.h"   
 
//Set EnglishWeight to 0.0. 
EnglishWeight::EnglishWeight() {
  pounds = 0;
  ounces = 0;
}
     
//Set EnglishWeight to a specified value.
EnglishWeight::EnglishWeight(int lbs, int ozs) {
  pounds = lbs;
  ounces = ozs;
}

int EnglishWeight::getPounds(){
  return this->pounds;
}

int EnglishWeight::getOunces(){
  return this->ounces;
}

EnglishWeight EnglishWeight::operator - (EnglishWeight &subtract_ew)  {
  int p1 = this->pounds - subtract_ew.pounds;
  int p2 = this->ounces - subtract_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight EnglishWeight::operator / (EnglishWeight &divide_ew)  {
  if(divide_ew == 0) 
    false;  
  else {
    int p1 = this->pounds / divide_ew.pounds;
    int p2 = this->pounds / divide_ew.ounces;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight EnglishWeight::operator * (const EnglishWeight& mult_ew, const int number) {
  int p1 = mult_ew.pounds * number;
  int p2 = mult_ew.ounces * number;
  return EnglishWeight(p1, p2);
}

EnglishWeight EnglishWeight::operator * (const int number, const EnglishWeight& mult_ew) {
  int p1 = number * mult_ew.pounds;
  int p2 = number * mult_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight EnglishWeight::operator / (const EnglishWeight& divide_ew, const int number) {
  if (number == 0)
    false;
  else{
    int p1 = divide_ew.pounds / number;
    int p2 = divide_ew.ounces / number;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight EnglishWeight::operator + (EnglishWeight &add_ew)  { 
  int p1 = this->pounds + add_ew.pounds;
  int p2 = this->ounces + add_ew.ounces;
  return EnglishWeight(p1, p2);
}

// Define stream insertion operator for EnglishWeight.
ostream& operator << (ostream& str, EnglishWeight& ew_out) {
  str << ew_out.pounds << "lbs " << ew_out.ounces << "ozs";
  return str;
}
 
// Define stream extraction operator for EnglishWeight.
istream& operator >> (istream& str, EnglishWeight& ew_in) {
  str >> ew_in.pounds;
  if (str.get() != '\'') {
   cerr << "*** Error with pounds. Incorrect format ***\n\n";
   exit(1);
  }
  str >> ew_in.ounces;
  if (str.get() != '"') {
   cerr << "*** Error with ounces. Incorrect format ***\n\n";
   exit(1);
  } return str;
}


and the errors generated:
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:40:16: error: invalid operands to binary expression ('EnglishWeight' and 'int')
  if(divide_ew == 0) 
     ~~~~~~~~~ ^  ~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:49:30: error: overloaded 'operator*' must be a unary or binary operator (has 3 parameters)
EnglishWeight EnglishWeight::operator * (const EnglishWeight& mult_ew, const int number) {
                             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:55:30: error: overloaded 'operator*' must be a unary or binary operator (has 3 parameters)
EnglishWeight EnglishWeight::operator * (const int number, const EnglishWeight& mult_ew) {
                             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:61:30: error: overloaded 'operator/' must be a binary operator (has 3 parameters)
EnglishWeight EnglishWeight::operator / (const EnglishWeight& divide_ew, const int number) {
                             ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:63:5: warning: expression result unused [-Wunused-value]
    false;
    ^~~~~
1 warning and 4 errors generated.
[Finished in 0.3s with exit code 1]


Thank you for walking me through this! you're amazing!!
i've never used "friend" before. But from what I've gathered, the functions you've declared as friends, should not be attached to the class. Meaning -

EnglishWeight EnglishWeight::operator * (const int number, const EnglishWeight& mult_ew)

This function for example, should be without the
EnglishWeight::


Im guessing this function, and the other friend frunctions should look like this instead -

EnglishWeight operator * (const int number, const EnglishWeight& mult_ew)

Edit:

1
2
 if(divide_ew == 0) 
    false;  


I don't think this is valid. false; doesnt do anything. Remember the function is suppose to return a type of EnglishWeight.

Edit: Also if(divide_ew == 0) I don't think this works. divide_ew is of type EnglishWeight and you're trying to check if it's equal to an integer? divide_ew will never be equal to 0 since it's not even a number. so you can remove that if statement entirely.
Last edited on
You're a rock star! I'm in love with this website already!!!!!!! I'm down to one error and one warning:

/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:40:16: error: invalid operands to binary expression ('EnglishWeight' and 'int')
  if(divide_ew == 0) 
     ~~~~~~~~~ ^  ~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment3/english_weights.cpp:63:5: warning: expression result unused [-Wunused-value]
    false;
    ^~~~~
1 warning and 1 error generated.
[Finished in 0.3s with exit code 1]


here's the .cpp class:
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
#include <iostream>  
#include <stdlib.h>
#include <math.h>
#include "english_weights.h"   
 
//Set EnglishWeight to 0.0. 
EnglishWeight::EnglishWeight() {
  pounds = 0;
  ounces = 0;
}
     
//Set EnglishWeight to a specified value.
EnglishWeight::EnglishWeight(int lbs, int ozs) {
  pounds = lbs;
  ounces = ozs;
}

int EnglishWeight::getPounds(){
  return this->pounds;
}

int EnglishWeight::getOunces(){
  return this->ounces;
}

EnglishWeight EnglishWeight::operator - (EnglishWeight &subtract_ew)  {
  int p1 = this->pounds - subtract_ew.pounds;
  int p2 = this->ounces - subtract_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight EnglishWeight::operator / (EnglishWeight &divide_ew)  {
  if(divide_ew == 0) 
    false;  
  else {
    int p1 = this->pounds / divide_ew.pounds;
    int p2 = this->pounds / divide_ew.ounces;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight operator * (const EnglishWeight& mult_ew, const int number) {
  int p1 = mult_ew.pounds * number;
  int p2 = mult_ew.ounces * number;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator * (const int number, const EnglishWeight& mult_ew) {
  int p1 = number * mult_ew.pounds;
  int p2 = number * mult_ew.ounces;
  return EnglishWeight(p1, p2);
}

EnglishWeight operator / (const EnglishWeight& divide_ew, const int number) {
  if (number == 0)
    false;
  else{
    int p1 = divide_ew.pounds / number;
    int p2 = divide_ew.ounces / number;
    return EnglishWeight(p1, p2);
  }
}

EnglishWeight EnglishWeight::operator + (EnglishWeight &add_ew)  { 
  int p1 = this->pounds + add_ew.pounds;
  int p2 = this->ounces + add_ew.ounces;
  return EnglishWeight(p1, p2);
}

// Define stream insertion operator for EnglishWeight.
ostream& operator << (ostream& str, EnglishWeight& ew_out) {
  str << ew_out.pounds << "lbs " << ew_out.ounces << "ozs";
  return str;
}
 
// Define stream extraction operator for EnglishWeight.
istream& operator >> (istream& str, EnglishWeight& ew_in) {
  str >> ew_in.pounds;
  if (str.get() != '\'') {
   cerr << "*** Error with pounds. Incorrect format ***\n\n";
   exit(1);
  }
  str >> ew_in.ounces;
  if (str.get() != '"') {
   cerr << "*** Error with ounces. Incorrect format ***\n\n";
   exit(1);
  } return str;
}
The error is still what I said at the last part of my last post.

1
2
if(divide_ew == 0) 
    false;  


That makes no sense because divide_ew is of type EnglishWeight. It's not a number, so it can never be equal to 0 right?

Even if it was equal to 0 somehow, false; doesnt do anything. You can remove it that entire if statement.

1
2
3
4
5
6
EnglishWeight EnglishWeight::operator / (EnglishWeight &divide_ew)  
{
    int p1 = this->pounds / divide_ew.pounds;
    int p2 = this->pounds / divide_ew.ounces;
    return EnglishWeight(p1, p2);
}


Hope this fixes the problem :)
sorry, i got too excited and didn't finish reading your message. I see what you are saying. I don't know what to do since the assignment says that I ahem to take care of special situations such as wrongful division by zero.
here is the official assignment for this module:

In the English system of weights, we have pounds and ounces, with 1 pound equal to 16 ounces.

Write a class to implement English weights. The type name of the class must be EnglishWeight.
Declare the class in a header file called EnglishWeight.h and implement the class in separate file called EnglishWeight.cpp.
Include appropriate constructors.
Include new versions of the stream insertion and stream extraction operators appropriate to this class.
Provide the following operators:

EnglishWeight + EnglishWeight

EnglishWeight - EnglishWeight

EnglishWeight / EnglishWeight (this one takes some thought!)

EnglishWeight * number

number * EnglishWeight

Englishweight / number
Write a small test program in a file called EW_Driver.cpp to allow a user to enter two weights (in pounds and ounces) and a number. The program will output the results of the six operations described above.
You know, I might just be a retard :D I've only used operator overloading once and that was months ago.

I guess divide_ew can be equal to 0. In that case yes you have to check if(divide_ew == 0)

But the problem is still the false; part. I get that you have to take care of special situations like division by zero. Tell me with words what you want to happen if that happened, what is false; suppose to represent, what did you think it would do? The thing is, if division by 0 occurs, you will have to return something. You can print out an error message but the return type has to be of type EnglishWeight.
What you could do is throw an exception, for when division by zero occurs. You can read and watch about it here -

http://www.cprogramming.com/tutorial/exceptions.html


This video shows how to handle division by 0 :) - https://www.youtube.com/watch?v=5369xtKS42s
Thank you so so much! and you are NOT a retard!!!! You completely helped me debug everything tonight!!! You're the bestttttttt!!!!!!!!!!!!!!!
THANK YOU!
You're very welcome :) Goodluck with the rest :)
@TarikNeaj

The OP is trying to check for division by zero.

@OP

But this, (as opposed to the scalar division later):

1
2
3
4
5
6
7
8
9
EnglishWeight EnglishWeight::operator / (EnglishWeight &divide_ew)  {
  if(divide_ew == 0) 
    false;  
  else {
    int p1 = this->pounds / divide_ew.pounds;
    int p2 = this->pounds / divide_ew.ounces;
    return EnglishWeight(p1, p2);
  }
}


If one is dividing by the same type, shouldn't the result be a scalar? Meaning the return type should be double. And that is not how to do division of the same type. For example: 12 feet 6 inches divided by 4 feet 0 inches is not 3.0 . So think about it like you are doing math with fractions. Same applies to all the math you do to your type.

Also, there is integer division everywhere - that won't work, make the types in the class definition double.

If you want to access members of an object from a non member function, use it's interface functions getPounds or getOunces .

Consider using a member initialiser list, rather than assignment:

1
2
3
4
5
6
7
8
9
10
11
//Set EnglishWeight to a specified value.
     // const function parameters, unsigned a better type
EnglishWeight::EnglishWeight(const unsigned int lbs, const unsigned int ozs)
                  :   // colon introduces initialiser list
                  pounds(static_cast<double>(lbs)),  // cast to double to suit class definition
                  ounces(static_cast<double>(ozs))
 {
     // check to see if values are valid
     // invariant 0 <= ozs < 16
}


Your accessor (get) functions should be const, they don't change the state of the class:

1
2
3
4
double EnglishWeight::getOunces() const {
  //return this->ounces; // use of this unnecessary here
    return ounces;
}



When including STL headers, there is no .h suffix:
#include <cmath>

Good Luck !!

Edit:

Didn't see the previous 5 posts while replying :+), but what I have said still applies.
Last edited on
Always good with some extra good information, thanks :)
Thanks y'all so much!! I was able to complete the assignment correctly and submit it! I am so appreciative for all the help!
I hope you also took care of normalizing the results. For example, 1lb 13oz plus 2lb 9 oz is not 3 lb 22 oz, it's 4 lb, 6 oz. And if you subtract, you don't get 1 lb -4 oz, you get 0 lb 12oz.

Pages: 12