错误:“."标记之前的预期未限定ID//(结构) [英] error: expected unqualified-id before ‘.’ token //(struct)

查看:63
本文介绍了错误:“."标记之前的预期未限定ID//(结构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道怎么做并且已经完成了大部分代码,但我一直收到这个错误错误:在‘.’标记之前预期不合格的id".

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

在 Rational.h 中;

#ifndef RATIONAL_H#define RATIONAL_H使用命名空间标准;结构简化形式{int iSimplifiedNumerator;int iSimplifiedDenominator;};//我在这里有一个课程用于程序中的其他内容#万一

在 Rational.cpp 中;

#include #include "rational.h"使用命名空间标准;void Rational :: SetToReducedForm(int iNumerator, int iDenominator){int iGreatCommDivisor = 0;iGreatCommDivisor = GCD(iNumerator, iDenominator);//接下来的两行是我得到错误的地方ReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;ReducedForm.iSimplifiedDenominator = iDenominator/iGreatCommDivisor;};

解决方案

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

ReducedForm rf;rf.iSimplifiedNumerator = 5;

或者像这样将成员更改为static:

struct ReducedForm{静态 int iSimplifiedNumerator;静态 int iSimplifiedDenominator;};

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

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

这篇关于错误:“."标记之前的预期未限定ID//(结构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆