. Advertisement .
..3..
. Advertisement .
..4..
I get the “expected unqualified-id before ‘{‘ token” error as the title says. How can I fix it so the error goes away? Here is my detail:
#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
#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;
};
When I operated it, I received the error text:
error: expected unqualified-id before ‘.’ token
I appreciate any help from you.
The cause:
From your program, I realize that not all the struct’s members are static and you are accessing statically to the struct with a
.
, but it’s illegal, it should be::
.Solution:
ReducedForm
is the name of the struct; you must create an object (for example thestruct
orclass
) to use it. Let’s follow these steps:The
.
::
is not being used to access the struct statically. Neither are its membersstatic
. You can either instantiateReducedForm
.Change the
static
members like this:You must use
::
to access members. I strongly doubt that you’re trying to access.
in this case.