13 Replies - 310 Views - Last Post: 11 February 2012 - 05:35 PM Rate Topic: -----

#1 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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 add(Rational &other);
    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;
}

Rational Rational::add(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::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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: problem with calling method

Posted 11 February 2012 - 04:25 PM

View PostGDubz, 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.
Was This Post Helpful? 0
  • +
  • -

#10 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: problem with calling method

Posted 11 February 2012 - 04:36 PM

View PostBryston, 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.
Was This Post Helpful? 1
  • +
  • -

#12 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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..
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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

Was This Post Helpful? 2
  • +
  • -

#14 GDubz  Icon User is offline

  • D.I.C Head

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

Re: problem with calling method

Posted 11 February 2012 - 05:35 PM

Yep that was it, thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1