C++ 错误:'.' 之前的预期不合格 ID 令牌//(结构)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18062616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: expected unqualified-id before ‘.’ token //(struct)
提问by Blobiu5
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".
我知道怎么做并且已经完成了大部分代码,但我一直收到这个错误“错误:在'之前预期不合格的id'。” 令牌”。
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;
我已经声明了一个名为 ReducedForm 的结构,它包含简化的分子和分母,现在我想做的是将简化的值发送到这个结构。这是我的代码;
In Rational.h;
在 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;
在 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;
};
回答by Joseph Pla
You are trying to access the struct statically with a .
instead of ::
, nor are its members static
. Either instantiate ReducedForm
:
您正在尝试使用.
而不是静态访问结构::
,其成员也不是static
。要么实例化ReducedForm
:
ReducedForm rf;
rf.iSimplifiedNumerator = 5;
or change the members to static
like this:
或将成员更改为static
这样:
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 ;)
在后一种情况下,你必须访问成员::
而不是.
我非常怀疑后者是你想要的;)
回答by IanPudney
The struct's name is ReducedForm
; you need to make an object(instance of the struct
or class
) and use that. Do this:
结构的名称是ReducedForm
; 您需要创建一个对象(struct
or 的实例class
)并使用它。做这个:
ReducedForm MyReducedForm;
MyReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
MyReducedForm.iSimplifiedDenominator = iDenominator/iGreatCommDivisor;
回答by juanchopanza
ReducedForm
is a type, so you cannot say
ReducedForm
是一种类型,所以你不能说
ReducedForm.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;
You can only use the .
operator on an instance:
您只能.
在实例上使用运算符:
ReducedForm rf;
rf.iSimplifiedNumerator = iNumerator/iGreatCommDivisor;