# problem with calling method

Page 1 of 1

## 13 Replies - 373 Views - Last Post: 11 February 2012 - 05:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=266328&amp;s=a079271fe9cdcdac6058c738943e39ba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 GDubz

Reputation: 1
• Posts: 92
• Joined: 06-January 12

# problem with calling method

Posted 11 February 2012 - 03:33 PM

Im trying to call a method that evaluates if one fraction is less than another fraction and Im getting a compile error on the line I marked.
(By the way this is c++)
```#include "rational.h"

int main()
{

Rational a(20,2);

Rational b(4,2);

if(a.less(B)/>) cout << "true" << endl;

}

```

```class Rational
{
public:
int numerator;
int denominator;

Rational(int _numerator, int _denominator);
void print();
Rational sub(Rational &other);
Rational mul(Rational &other);
Rational div(Rational &other);
bool less(Rational &other);
int gcd(int x, int y);
};

```

```
#include <iostream>
#include "rational.h"
using namespace std;

Rational::Rational(int _numerator, int _denominator)
{
numerator = _numerator;
denominator = _denominator;
}

void Rational::print()
{
cout << numerator << "/" << denominator << endl;
}

{
int new_numerator = (numerator*other.denominator) + (denominator *other.numerator);
int new_denominator = (denominator * other.denominator);
int gcd = Rational::gcd(new_numerator, new_denominator);
return Rational(new_numerator/gcd, new_denominator/gcd);
}

Rational Rational::sub(Rational &other)
{
int new_numerator = (numerator*other.denominator) - (denominator *other.numerator);
int new_denominator = (denominator * other.denominator);
int gcd = Rational::gcd(new_numerator, new_denominator);
return Rational(new_numerator/gcd, new_denominator/gcd);
}

Rational Rational::mul(Rational &other)
{
int new_numerator = (numerator * other.numerator);
int new_denominator = (denominator * other.denominator);
int gcd = Rational::gcd(new_numerator, new_denominator);
return Rational(new_numerator/gcd, new_denominator/gcd);
}

Rational Rational::div(Rational &other)
{
int new_numerator = (numerator*other.denominator);
int new_denominator = (denominator * other.numerator);
int gcd = Rational::gcd(new_numerator, new_denominator);
return Rational(new_numerator/gcd, new_denominator/gcd);
}

bool Rational::less(Rational &other)
{
if((numerator*other.denominator)<(other.numerator*denominator)) return true;
else return false;
}

int Rational::gcd(int x, int y)
{
int g;
if (x < 0) x = -x;
if (y < 0) y = -y;
if ((x+y) == 0) return 0;
g = y;

while(x > 0)
{
g = x;
x = y % x;
y = g;
}

return g;
}

```

This post has been edited by GDubz: 11 February 2012 - 03:34 PM

Is This A Good Question/Topic? 0

## Replies To: problem with calling method

### #2 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 03:38 PM

Which line, and exactly what is the error?

### #3 GDubz

Reputation: 1
• Posts: 92
• Joined: 06-January 12

## Re: problem with calling method

Posted 11 February 2012 - 03:41 PM

ah sorry about that its line 10 in main, the bool less method

### #4 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 03:42 PM

You used B as the argument, although the object's name is b.

### #5 GDubz

Reputation: 1
• Posts: 92
• Joined: 06-January 12

## Re: problem with calling method

Posted 11 February 2012 - 03:46 PM

weird its not capital in my IDE, I think it might have to something with the method itself hmm

### #6 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 03:49 PM

OK, then the uppercase B is being caused by the DIC forum code. It happens when a b is followed by a ). You can avoid it by turning off emoticons in the "post options" section below the posting window.

But that's the only error I see in your code. So, if that's not the problem, exactly what is the error message you're getting?

### #7 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 04:03 PM

By the way, your algorithm for "less" will give the wrong result if one of the denominators is negative.

### #8 GDubz

Reputation: 1
• Posts: 92
• Joined: 06-January 12

## Re: problem with calling method

Posted 11 February 2012 - 04:18 PM

That's right Im gonna have to make a neg member method, but its a compile error, do you know how to get more information abotu errors in codeblocks IDE?

### #9 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 04:25 PM

GDubz, on 11 February 2012 - 06:18 PM, said:

That's right Im gonna have to make a neg member method

There's a simpler solution, but I'll let you think about that...

Quote

but its a compile error, do you know how to get more information abotu errors in codeblocks IDE?

This is like pulling teeth. I'm begging you -- tell me what the error message is. Post the exact message that CB is giving you.

### #10 Bryston

Reputation: 15
• Posts: 125
• Joined: 24-January 12

## Re: problem with calling method

Posted 11 February 2012 - 04:33 PM

I don't know if it would help but I have some code here that does something similar, it's from the text I am following as I learn. Appropriately enough it is from the chapter introducing classes and the example is all about normalizing fractions, gcf, lcm etc... and introduces the equality operator into the class. If you want to take a look at it let me know.

### #11 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 04:36 PM

Bryston, on 11 February 2012 - 06:33 PM, said:

I don't know if it would help but I have some code here that does something similar, it's from the text I am following as I learn. Appropriately enough it is from the chapter introducing classes and the example is all about normalizing fractions, gcf, lcm etc... and introduces the equality operator into the class. If you want to take a look at it let me know.

That wouldn't shed any light on a compile error -- but neither can I if I don't know what the compiler error message is.

### #12 GDubz

Reputation: 1
• Posts: 92
• Joined: 06-January 12

## Re: problem with calling method

Posted 11 February 2012 - 04:40 PM

it says cout and endl are not defined in this scope..

### #13 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: problem with calling method

Posted 11 February 2012 - 04:44 PM

Simple: you need to qualify them both with std::, or put using namespace std; in your main.cpp file.

Sorry, I assumed that what you posted was just an excerpt from your file. It didn't occur to me that that was the entire file.

By the way, that's not a "CodeBlocks problem". The same thing would occur in any IDE, or in no IDE at all -- compiling on the command line in a terminal window. (The only exception I can think of is Turbo-C++ -- but that doesn't count: it's not standard C++, not by a long shot.)

This post has been edited by r.stiltskin: 11 February 2012 - 04:58 PM

### #14 GDubz

Reputation: 1
• Posts: 92
• Joined: 06-January 12

## Re: problem with calling method

Posted 11 February 2012 - 05:35 PM

Yep that was it, thanks again