Trouble With Classes

Im trying to grasp the concept of Classes, Structure's and Object Orientated programming and im having a lot of trouble, i have this very simple bit of code that im getting an error on
#include <cstdlib>
#include <iostream>

using namespace std;
class Rational{
public:
Rational::Rational(double n,double d){
num = n;
denom = d;
}

void Rational::print(){
cout << num << "/" << denom << endl;
}
private:
double num,denom;
};
int main(int argc, char *argv[])
{
Rational Rat1 (2,1);
Rational Rat2 (4,5);
Rat1.print;
Rat2.print;
system("PAUSE");
return EXIT_SUCCESS;
}

im getting a "statement cannot resolve address of overloaded function" error for both Rat1.print and Rat2.print and can't understand why
Last edited on
Only use the scope function when defining member functions outside of the class, e.g.

1
2
3
4
5
6
7
8
9
class MyClass{
    public:
       MyClass();
       void funct1();  //Provide prototypes to let compiler know there is a member function
       void funct2();
};
MyClass::MyClass(){/*...*/}
void MyClass::funct1(){/*...*/}
void MyClass::funct2(){/*...*/}


Or just define the functions directly inside the class, though the prior example is more preferable.

1
2
3
4
5
6
class MyClass{
    public:
       MyClass(){/*...*/}
       void funct1(){/*...*/}
       void funct2(){/*...*/}
};
Last edited on
yeah, that must be your problem.

Aceix.
Alright I change where the scope occurs
#include <cstdlib>
#include <iostream>

using namespace std;
class Rational{
public:
Rational(double n,double d){
num = n;
denom = d;
}
void print(){
cout << num << "/" << denom<< endl;
}

private:
double num,denom;
};
int main(int argc, char *argv[])
{
Rational Rat1 (2,1);
Rational Rat2 (4,5);
Rat1::print;
Rat2::print;
system("PAUSE");
return EXIT_SUCCESS;
}

But im still getting errors saying Rat1 and Rat2 are not classes and that print is undeclared but arent Rat1 and Rat2 suppose to be Objects not classes?
Ok, when calling member functions, bo not forget the brackets as part of a function call syntax. So therefore: Rat.print();

HTH,
Aceix.
My bad i noticed that right after posting but im still getting the same errors
#include <cstdlib>
#include <iostream>

using namespace std;
class Rational{
public:
Rational(double n,double d){
num = n;
denom = d;
}
void print(){
cout << num << "/" << denom<< endl;
}

private:
double num,denom;
};
int main(int argc, char *argv[])
{
Rational Rat1 (2,1);
Rational Rat2 (4,5);
Rat1::print();
Rat2::print();
system("PAUSE");
return EXIT_SUCCESS;
}
Use the dot operator. . not ::

Aceix.
Last edited on
Finally! thanks man appreciate it
Topic archived. No new replies allowed.