Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: expected unqualified-id before ‘.’ token //(struct)

Tags:

c++

struct

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;
};
like image 918
Blobiu5 Avatar asked Aug 05 '13 16:08

Blobiu5


2 Answers

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 ;)

like image 194
Joseph Pla Avatar answered Sep 21 '22 21:09

Joseph Pla


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;
like image 28
IanPudney Avatar answered Sep 24 '22 21:09

IanPudney