Class problem (does not name type)

I keep getting the error code for all of my class function definitions. It just says does not name a type I have looked around and found no solutions I was thinking it was because they have no type definition, but I thought that the Rational constructor did that in its stead. Any input?

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
#include <iostream>

using namespace std;

class Rational //Rational Number Class
	{
		public :


     Rational ( int initNum, unsigned int initDenom ) ;

    int getNumerator ( ) ;

    unsigned int getDenominator ( ) ;

    Rational add ( Rational secondAddend ) ;

    Rational sub ( Rational subtractor);

    Rational mul ( Rational secondFactor) ;

    Rational div (Rational  divisor) ;

    Rational Less ( Rational greaterThan) ;

    void output ( ostream & outputStream ) ;


		private :

			int numerator;
			unsigned int denominator;

	};

int main ()
{
        Rational Rational (2, 5);
        Rational.output(cout);

    return 0;

}

Rational Rational (int initNumerator, unsigned int initDenominator)//class definitions{


				{
				int numerator = initNumerator;
				int denominator = initDenominator;
				}

			 Rational void Rational:: output (ostream & anOstream)
				{
				anOstream << numerator << '/' << denominator;
				}

			 Rational Rational::getNumerator ()
				{return this -> numerator;}

			 Rational Rational::getDenominator ()
				{return this -> denominator;}

			 Rational Rational::add (Rational secondAddend)
			{
			    Rational result
				((this -> numerator * secondAddend.denominator + this -> denominator * secondAddend.numerator),
                (this -> denominator * secondAddend.denominator));

                return result;
			}

			 Rational Rational::sub (Rational subtractor)
			{
				Rational result;
                    ((this -> numerator * subtractor.denominator - this -> denominator * subtractor.numerator),
                    (this -> denominator * subtractor.denominator));

                return result;
			}

			 Rational Rational::mul (Rational secondFactor)
			{
			    Rational result
                    ((this -> numerator * secondFactor.numerator),
                    (this -> denominator * secondFactor.denominator);

                return result;
			}

			 Rational Rational::div ( Rational divisor )
                {
                    Rational result
                        ((this->numerator   * divisor.denominator),
                        (this->denominator * divisor.numerator )) ;

                    return result ;
                } // end operator/ function


         bool Rational::Less== (Rational greaterThan)
                    {
			   Rational result
                        ((this -> numerator / this -> denominator ) < (greaterThan.numerator / greaterThan.denominator)),
                    (this -> numerator * greaterThan.denominator) < (greaterThan.numerator * this -> denominator));

                  return result;
                    }




Heya! Couple quick things:

1. Rational Rational (2, 5);
unfortunately you can't name an instance of your class the same as the name of your class, you could try Rational pumpkinWizard(13,37); instead!

2. You're missing your scope operator in your constructor implementation Rational Rational (int initNumerator, unsigned int initDenominator) (try Rational::Rational instead of Rational Rational
OP, your project is the same as this guy's:
http://www.cplusplus.com/forum/beginner/156953/
Maybe something in there can help you, too.
It helped me in some ways, but not my overall problem I fixed what ultifinitus said and it gave me a whole slew of errors. I used your header because I liked it more than mine though! All the errors now are saying not enough arguments supplied. Would you run it real quick and see if you have the time?


//edit It seems the errors are in my getNumerator (), getDenominator (), and Output() functions. It says they do not match any in class. Also, sorry my code is so offset in my ide it looks normal.


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
#include <iostream>

using namespace std;

class Rational
{
public :
    Rational(int initNumerator, unsigned int initDenominator );
    Rational(int initNumerator);
    int getNumerator() ;
    unsigned int getDenominator() ;
    Rational add(const Rational& addend) ;
    Rational sub(const Rational& subtractor);
    Rational mul(const Rational& factor) ;
    Rational div(const Rational& divisor) ;
    bool less(const Rational& rhs) ;
    bool equals(const Rational& rhs) ;
    Rational neg() ;
    void output (ostream& outputStream);

private :
    int           numerator;
    unsigned int  denominator;
};


int main ()
{
        Rational twofiths (2, 5);


        return 0;

}

Rational::Rational (int initNumerator, unsigned int initDenominator)//class definitions{
				{
				 numerator = initNumerator;
				 denominator = initDenominator;
				}

			Rational Rational::output (ostream & anOstream)
				{
				anOstream << numerator << '/' << denominator;
				}

			 Rational Rational::getNumerator ()
				{return (this -> numerator;)}

			 Rational Rational::getDenominator ()
				{return (this -> denominator;)}

			 Rational Rational::add (const Rational& addend)
			{
			    Rational result
				((this -> numerator * addend.denominator + this -> denominator * addend.numerator),
                (this -> denominator * addend.denominator));

                return result;
			}

			 Rational Rational::sub (const Rational& subtractor)
			{
				Rational result
                    ((this -> numerator * subtractor.denominator - this -> denominator * subtractor.numerator),
                    (this -> denominator * subtractor.denominator));

                return result;
			}

			 Rational Rational::mul (const Rational& factor)
			{
			    Rational result
                    ((this -> numerator * factor.numerator),
                    (this -> denominator * factor.denominator));

                return result;
			}

			Rational Rational::div ( const Rational& divisor )
                {
               Rational result
                        ((this->numerator   * divisor.denominator),
                        (this->denominator * divisor.numerator )) ;

                    return result ;
                } // end operator/ function


        bool Rational Rational::less(const Rational& rhs) const
            {
                int leftHandSide = this->numerator * rhs.getDenominator();
                int rightHandSide = rhs.getNumerator() * this->denominator;

                return leftHandSide < rightHandSide;
            }




Last edited on
Using consistent indentation is helpful when trying to read the code. I've made some comments in the code to point out some of your errors. Correct these and see if it helps.
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
#include <iostream>
using namespace std;

class Rational
{
public :
    Rational(int initNumerator, unsigned int initDenominator);
    Rational(int initNumerator);
    int getNumerator();
    unsigned int getDenominator();
    Rational add(const Rational& addend);
    Rational sub(const Rational& subtractor);
    Rational mul(const Rational& factor);
    Rational div(const Rational& divisor);
    bool less(const Rational& rhs);
    bool equals(const Rational& rhs);
    Rational neg();
    void output (ostream& outputStream);

private :
    int           numerator;
    unsigned int  denominator;
};

int main ()
{
    Rational twofiths (2, 5);
    return 0;
}

Rational::Rational (int initNumerator, unsigned int initDenominator)//class definitions
{
    numerator = initNumerator;
    denominator = initDenominator;
}

//this doesn't match... in the class declaration
//this method returns void, not Rational
Rational Rational::output (ostream & anOstream)
{
    anOstream << numerator << '/' << denominator;
}

//this doesn't match... in the class declaration
//this method returns int, not Rational
Rational Rational::getNumerator ()
{return (this -> numerator;)} //take the parentheses out

//this doesn't match... in the class declaration
//this method returns unsigned int, not Rational
Rational Rational::getDenominator ()
{return (this -> denominator;)} //take the parentheses out

Rational Rational::add (const Rational& addend)
{
    Rational result
        ((this -> numerator * addend.denominator + this -> denominator * addend.numerator),
        (this -> denominator * addend.denominator));

    return result;
}

Rational Rational::sub (const Rational& subtractor)
{
    Rational result
        ((this -> numerator * subtractor.denominator - this -> denominator * subtractor.numerator),
        (this -> denominator * subtractor.denominator));

    return result;
}

Rational Rational::mul (const Rational& factor)
{
    Rational result
        ((this -> numerator * factor.numerator),
        (this -> denominator * factor.denominator));

    return result;
}

Rational Rational::div ( const Rational& divisor )
{
    Rational result
        ((this->numerator   * divisor.denominator),
        (this->denominator * divisor.numerator )) ;

    return result ;
} // end operator/ function

//which does this return? bool or Rational? You have two types
//here where there should only be one
bool Rational Rational::less(const Rational& rhs) const
{
    int leftHandSide = this->numerator * rhs.getDenominator();
    int rightHandSide = rhs.getNumerator() * this->denominator;

    return leftHandSide < rightHandSide;
}
Okay cool making progress thank you. Why on lines 47 and 52 does it matter if the parentheses are there?

1
2
3
4
5
6
7
8

bool  Rational::less(const Rational& rhs)
            {
                int leftHandSide = this->numerator * rhs.getDenominator();
                int rightHandSide = rhs.getNumerator() * this->denominator;

                return leftHandSide < rightHandSide;
            }


I have corrected the boolean issue but I still get the error

|99|error: passing 'const Rational' as 'this' argument of 'unsigned int Rational::getDenominator()' discards qualifiers [-fpermissive]|

edit//

int leftHandSide = this->numerator * rhs.getDenominator();
int rightHandSide = rhs.getNumerator() * this->denominator;

replaced with

int leftHandSide = this->numerator * rhs.denominator;
int rightHandSide = rhs.numerator * this->denominator;

return leftHandSide < rightHandSide;

Now it builds fine I just have to test it which is another story.
Last edited on
Hmm now I have even more problems. All I added was a test to my main and it will no longer build. Line 36.

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
int main ()
{

#include <iostream>

using namespace std;

class Rational
{
public :
    Rational(int initNumerator, unsigned int initDenominator );
    Rational(int initNumerator);
    int getNumerator() ;
    unsigned int getDenominator() ;
    Rational add(const Rational& addend) ;
    Rational sub(const Rational& subtractor);
    Rational mul(const Rational& factor) ;
    Rational div(const Rational& divisor) ;
    bool less(const Rational& rhs) ;
    Rational neg() ;
    void output (ostream& outputStream);
    void input (istream& inputStream);
private :
    int           numerator;
    unsigned int  denominator;
    char c;
};


int main ()
{

    Rational a (1,4);
    Rational b (3,4);

    a.add(b).output(cout);//correct output given
    a.sub(b).output(cout);//correct output given
    a.mul(b).output(cout);//correct output given
    a.div(b).output(cout);//correct output given
    a.less(b).output(cout); 
}

Rational::Rational (int initNumerator, unsigned int initDenominator)//class definitions{
				{
				 numerator = initNumerator;
				 denominator = initDenominator;
				}

	void Rational::output (ostream & anOstream)
            {
		anOstream << numerator << '/' << denominator;
            }

        void Rational:: input (istream& inputStream)
            {
                inputStream >> numerator >> c >> denominator;
            }

        int Rational::getNumerator ()
				{return this -> numerator;}

        unsigned int Rational::getDenominator ()
				{return this -> denominator;}

        Rational Rational::add (const Rational& addend)
		{
			    Rational result
				((this -> numerator * addend.denominator + this -> denominator *              addend.numerator),
                (this -> denominator * addend.denominator));

                return result;
			}

	 Rational Rational::sub (const Rational& subtractor)
			{
				Rational result
                    ((this -> numerator * subtractor.denominator - this -> denominator * subtractor.numerator),
                    (this -> denominator * subtractor.denominator));

                return result;
			}

	Rational Rational::mul (const Rational& factor)
			{
			    Rational result
                    ((this -> numerator * factor.numerator),
                    (this -> denominator * factor.denominator));

                return result;
			}

	Rational Rational::div ( const Rational& divisor )
                {
                    Rational result
                        ((this->numerator   * divisor.denominator),
                        (this->denominator * divisor.numerator )) ;

                    return result ;
                } // end operator/ function

        bool  Rational::less(const Rational& rhs)
            {
                int leftHandSide = this->numerator * rhs.denominator;
                int rightHandSide = rhs.numerator * this->denominator;

                return leftHandSide < rightHandSide;
            }
        Rational Rational::neg()
            {
                return Rational( -(this->numerator), this->denominator);
            }




}


edit// I wasn't using my output stream to print sorry I keep jumping the gun on asking for help :p. My functions are testing bad now so I need to find out what is wrong with my math.
Last edited on
Please copy and paste the exact error message and indicate which line it is referring to.
In the int main line 40. I want to print the true/false value of the less function what would be the correct syntax for that line. Also if I wanted to include a reduce function would I declare that in the public section or private?
less() returns a bool, so you would output it the same way you output any other primitive type:

std::cout << a.less(b) << std::endl;
Would there be a way to use my output stream function to print it or is this the best way to implement it?

Also, I have declared the following function to reduce my fraction.

1
2
3
4
5
6
7
8
 void reduce(const Rational& reduced)
            {
                int denominator, numerator;
                for (int i = denominator * numerator; i > 1; i--) {
                    if ((denominator % i == 0) && (numerator % i == 0)) {
                    denominator /= i;
                    numerator /= i;
            }}}


If I wanted to add that to line 36 how what would be a way to do that?
lew13 wrote:
Would there be a way to use my output stream function to print it or is this the best way to implement it?
A bool has no relation to your class or your code at all. You cannot change its behavior.
lew13 wrote:
Also, I have declared the following function to reduce my fraction.
Why do you have that parameter? You don't even use it.
lew13 wrote:
If I wanted to add that to line 36 how what would be a way to do that?
That depends on how you fix the problem I pointed out above.
I am trying to implement the reduce function before it prints in the cout here is my updated 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
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
int main ()
{

#include <iostream>

using namespace std;

class Rational
{
public :
    Rational(int initNumerator, unsigned int initDenominator );
    Rational(int initNumerator);
    int getNumerator() ;
    unsigned int getDenominator() ;
    Rational add(const Rational& addend) ;
    Rational sub(const Rational& subtractor);
    Rational mul(const Rational& factor) ;
    Rational div(const Rational& divisor) ;
    bool less(const Rational& rhs) ;
    Rational neg() ;
    void output (ostream& outputStream);
    void input (istream& inputStream);
private :
    int           numerator;
    unsigned int  denominator;
    char c;
    void reduce (const Rational& reduced);
};


int main ()
{

    Rational a (1,4);
    Rational b (3,4);

    a.add(b).output(cout);//correct output given
    a.sub(b).output(cout);//correct output given
    a.mul(b).output(cout);//correct output given
    a.div(b).output(cout);//correct output given
    a.less(b).output(cout); 
}

Rational::Rational (int initNumerator, unsigned int initDenominator)//class definitions{
				{
				 numerator = initNumerator;
				 denominator = initDenominator;
				}
       Rational::Rational(int initNumerator)
            {
                numerator = initNumerator;
                denominator = 1;

            }
	void Rational::output (ostream & anOstream)
            {
		anOstream << numerator << '/' << denominator;
            }

        void Rational:: input (istream& inputStream)
            {
                inputStream >> numerator >> c >> denominator;
            }

        int Rational::getNumerator ()
				{return this -> numerator;}

        unsigned int Rational::getDenominator ()
				{return this -> denominator;}

        Rational Rational::add (const Rational& addend)
		{
			    Rational result
				((this -> numerator * addend.denominator + this -> denominator *              addend.numerator),
                (this -> denominator * addend.denominator));

                return result;
			}

	 Rational Rational::sub (const Rational& subtractor)
			{
				Rational result
                    ((this -> numerator * subtractor.denominator - this -> denominator * subtractor.numerator),
                    (this -> denominator * subtractor.denominator));

                return result;
			}

	Rational Rational::mul (const Rational& factor)
			{
			    Rational result
                    ((this -> numerator * factor.numerator),
                    (this -> denominator * factor.denominator));

                return result;
			}

	Rational Rational::div ( const Rational& divisor )
                {
                    Rational result
                        ((this->numerator   * divisor.denominator),
                        (this->denominator * divisor.numerator )) ;

                    return result ;
                } // end operator/ function

        bool  Rational::less(const Rational& rhs)
            {
                int leftHandSide = this->numerator * rhs.denominator;
                int rightHandSide = rhs.numerator * this->denominator;

                return leftHandSide < rightHandSide;
            }
        Rational Rational::neg()
            {
                return Rational( -(this->numerator), this->denominator);
            }


         void reduce(const Rational& reduced)
             {
                unsigned int denominator;
                int numerator;
                for (int i = denominator * numerator; i > 1; i--) {
                    if ((denominator % i == 0) && (numerator % i == 0)) {
                    denominator /= i;
                    numerator /= i;
            }}}

}
Last edited on
Did you see my post?
Yeah I thought I answered it. I guess I don't understand what you meant.
Oh so you're saying it should look like this:


void reduce()
          {
             unsigned int denominator;
             int numerator;
              for (int i = denominator * numerator; i > 1; i--) {
                    if ((denominator % i == 0) && (numerator % i == 0)) {
                      denominator /= i;
                      numerator /= i;
            }}}
Yes. I have no idea if that is correct, but I can say it is at least declared properly.
Last edited on
Any input on how to implement the reduce function?
1. Find the greatest common divisor (GCD) of the numerator and denominator.
2. Divide the numerator by the GCD to get the new numerator.
3. Divide the denominator by the GCD to get the new denominator.
Topic archived. No new replies allowed.