错误:'.'之前的预期unqualified-id 令牌//(结构)

Blo*_*iu5 7 c++ struct

我需要制作一个程序,从用户那里得到一小部分,然后简化它.

我知道如何做,并完成了大部分代码,但我不断收到此错误"错误:预期未经过资格的ID'.' 令牌".

我已经声明了一个名为ReducedForm的结构,它包含简化的分子和分母,现在我想要做的是将简化值发送到这个结构.这是我的代码;

在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
Run Code Online (Sandbox Code Playgroud)

在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;
};
Run Code Online (Sandbox Code Playgroud)

Jos*_*Pla 5

您正在尝试使用.而不是静态访问结构::,其成员也不是static。要么实例化ReducedForm

ReducedForm rf;
rf.iSimplifiedNumerator = 5;
Run Code Online (Sandbox Code Playgroud)

或将成员更改为static这样:

struct ReducedForm
{
    static int iSimplifiedNumerator;
    static int iSimplifiedDenominator;
};
Run Code Online (Sandbox Code Playgroud)

在后一种情况下,你必须访问成员::而不是.我非常怀疑后者是你想要的;)


Ian*_*ney 5

该结构的名称是ReducedForm;您需要创建一个对象struct(或 的实例class)并使用它。做这个:

ReducedForm MyReducedForm;
MyReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
MyReducedForm.iSimplifiedDenominator = iDenominator/iGreatCommDivisor;
Run Code Online (Sandbox Code Playgroud)


jua*_*nza 3

ReducedForm是一种类型,所以你不能说

ReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
Run Code Online (Sandbox Code Playgroud)

您只能.在实例上使用该运算符:

ReducedForm rf;
rf.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
Run Code Online (Sandbox Code Playgroud)