I need to make a program that gets a fraction from the user and then simplifies it.
I know how to do it and have done most of the code but I keep getting this error "error: expected unqualified-id before ‘.’ token".
I have declared a struct called ReducedForm which holds the simplified numerator and denominator, now what Im trying to do is send the simplified values to this struct. Here is my code;
In Rational.h;
#ifndef RATIONAL_H
#define RATIONAL_H
using namespace std;
struct ReducedForm
{
int iSimplifiedNumerator;
int iSimplifiedDenominator;
};
//I have a class here for the other stuff in the program
#endif
In Rational.cpp;
#include <iostream>
#include "rational.h"
using namespace std;
void Rational :: SetToReducedForm(int iNumerator, int iDenominator)
{
int iGreatCommDivisor = 0;
iGreatCommDivisor = GCD(iNumerator, iDenominator);
//The next 2 lines is where i get the error
ReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
ReducedForm.iSimplifiedDenominator = iDenominator/iGreatCommDivisor;
};
You are trying to access the struct statically with a .
instead of ::
, nor are its members static
. Either instantiate ReducedForm
:
ReducedForm rf;
rf.iSimplifiedNumerator = 5;
or change the members to static
like this:
struct ReducedForm
{
static int iSimplifiedNumerator;
static int iSimplifiedDenominator;
};
In the latter case, you must access the members with ::
instead of .
I highly doubt however that the latter is what you are going for ;)
The struct's name is ReducedForm
; you need to make an object (instance of the struct
or class
) and use that. Do this:
ReducedForm MyReducedForm;
MyReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
MyReducedForm.iSimplifiedDenominator = iDenominator/iGreatCommDivisor;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With