需要帮助理解struct,和程序的一些错误 [英] Need help understanding struct, and a few errors with program
问题描述
我没有得到如何正确使用结构来实现我的目标,计算分数(它是必需的)。坦白地说,我对我所做的事没有多少了解,这只是我在C ++中的第三个课,我感到迷失...这是分配给我们的任务
您的enter()函数接受用户的一个分数。你的
simplify()函数简化它接收的分数,如果
可能。您的display()函数显示
接收的分数。
您的全局函数使用Fraction类型。分数类型将分数的
分子和分母保存为单独的数据成员。
这是我的程序, 主除了 和 cout ,GCF函数由教授提供,所有其他函数和struct我试图做自己...
#include< iostream>
using namespace std;
void entry(int a,int b);
void simplify(double c);
void display(int x,int y)
int main()
{
struct Fraction fraction;
cout<< 输入分子:< endl;
cin>>分数。
cout<< 输入分母:< endl;
cin>>分数。
cout<< 分数简化器< endl;
cout<< ===================< endl;
enter(& fraction);
simplify(& fraction);
display(fraction);
}
struct Fraction {
int num;
int den;
}
struct分数分数{
fraction.num;
fraction.den;
}
void display(int num,int den){
cout<分数。 endl;
cout<<分数。 endl;
}
//由教授提供的大公因子(Euclid算法)
int gcf(int num1,int num2 )
{
int remainder = num2%num1;
if(remainder!= 0)
{
return gcf(remainder,num1);
}
return num1;
}
这些是我的错误:
$ b b
w2.cpp:在函数'int main()':
w2.cpp:14:error:aggregate'Fraction fraction'具有不完整的类型,
w2.cpp:23:错误:'enter'未在此范围中声明
w2.cpp:在全局范围:w2.cpp:35:error:函数定义不声明参数
w2.cpp:在函数'void display(int,int)':
w2.cpp:41:error:'fraction'未在此范围中声明
对不起,真的很长的帖子,但任何和所有的帮助是非常感谢。
如果有人可以给我一个有用的C ++书,我可以在家里和讲座(因为语言障碍我不能理解我的教授足够好)阅读
错误:
现在,在 main()
,你说 struct Fraction fraction;
。在这一点上,你是向前声明你的结构体。它不完整,所以你不能使用它,如果它是。
您应该在 main()之前定义整个
结构
$ c>。还要注意, struct
在 struct Fraction fraction;
是不必要的,并留在C。
错误:'enter'未在此范围内声明
简单。您已经向上声明 entry()
,但您尝试使用 enter()
。
在全局范围:w2.cpp:35:error:函数定义不声明参数
现在这有点混乱。这是冒犯行:
struct Fraction fraction {
编译器如何看到这是一个返回 Fraction
的函数,但是它缺少其参数列表。我不太清楚你要对这段代码做什么。
错误:'fraction'不是在这个范围中声明
看起来你试图使用在其他地方声明的对象。如果你想要从 main()
,你必须传递它作为参数。如果你想要一个全局变量 fraction
,你在全局空间中需要的是:
馏分;
这应该发生在 Fraction
结构之后。还要注意,因为它与 main()
中的那个具有相同的对象名, main()
阴影这一个,如果你想访问从 main()
的全局一个,你需要使用 :: fraction
我希望有助于澄清一些理解。
/ p>
输入(& fraction);
您正在传送 Fraction *
一个函数需要两个 int
s。我想你想让这个人拿一个部分&
。然后你可以像 enter(fraction);
调用它来修改传入的对象。
simplify(& fraction);
类似,但这个需要一个 double
。
- >
条目和
简化
函数从未定义,但仍尝试使用它们。 -
显示
应该需要一个部分
才能打印它的部分。
I don't get how to use structs properly to achieve my goal of calculating Fractions (it is required). Quite frankly I don't have much of an idea of what I'm doing, this is only my 3rd class in C++ and I feel lost...this was the task assigned to us
Your enter() function accepts a fraction from the user. Your simplify() function simplifies the fraction that it receives, if possible. Your display() function displays the fraction that it receives.
Your global functions use a Fraction type. A Fraction type holds the numerator and denominator of a fraction as separate data members.
This is my program, only the main EXCEPT the "cin" and "cout" and the GCF function was provided by the professor, all other functions and struct outside of the main i tried to do myself...
#include <iostream>
using namespace std;
void entry (int a, int b);
void simplify (double c);
void display(int x, int y)
int main()
{
struct Fraction fraction;
cout << "Enter a numerator: " << endl;
cin >> fraction.num;
cout << "Enter a denominator: " << endl;
cin >> fraction.den;
cout << "Fraction Simplifier" << endl;
cout << "===================" << endl;
enter(&fraction);
simplify(&fraction);
display(fraction);
}
struct Fraction {
int num;
int den;
}
struct Fraction fraction{
fraction.num;
fraction.den;
}
void display(int num, int den) {
cout << fraction.num << endl;
cout << fraction.den << endl;
}
// Great Common Factor (Euclid's Algorithm), provided by Professor
int gcf( int num1, int num2 )
{
int remainder = num2 % num1;
if ( remainder != 0 )
{
return gcf( remainder,num1 );
}
return num1;
}
these are my errors:
w2.cpp: In function 'int main()':
w2.cpp: 14: error: aggregate 'Fraction fraction' has incomplete type and cannot be defined
w2.cpp: 23: error: 'enter' was not declared in this scope
w2.cpp: At global scope: w2.cpp:35: error: function definition does not declare parameters
w2.cpp: In function 'void display(int, int)':
w2.cpp: 41: error: 'fraction' was not declared in this scope
I'm sorry for the really long post, but any and all help is greatly appreciated. AND if someone could point me to a helpful C++ book that I could read while at home and or in lectures (because of a language barrier i cannot understand my prof well enough) would also be appreciated
Let's walk through these:
error: aggregate 'Fraction fraction' has incomplete type and cannot be defined
Now, in main()
, you said struct Fraction fraction;
. At this point, you're forward-declaring your struct. It is not complete, so you can't use it as if it were.
You should have your whole Fraction
struct defined before main()
. Also note that the struct
in struct Fraction fraction;
is unnecessary, and left over from C.
error: 'enter' was not declared in this scope
Simple. You've declared entry()
up top, but you're trying to use enter()
. Not much more to be said.
At global scope: w2.cpp:35: error: function definition does not declare parameters
Now this is a bit more confusing. This is the offending line:
struct Fraction fraction{
How the compiler sees this is that it is a function returning a Fraction
, but it's missing its parameter list. I'm not exactly sure what you're trying to do with this block of code.
error: 'fraction' was not declared in this scope
Looks like you're trying to use an object declared somewhere else. If you want the one from main()
, you'll have to pass it in as an argument. If you want a global variable fraction
, all you need in the global space is:
Fraction fraction;
This should occur after the Fraction
struct. Also note that because this has the same object name as the one in main()
, the one in main()
shadows this one, and if you want to access the global one from main()
you need to use ::fraction
.
I hope that helps clear up some of the understanding.
Some other errors I see are:
enter(&fraction);
You're passing a Fraction *
to a function that takes two int
s. I think you'd want this one to take a Fraction &
. Then you can just call it like enter (fraction);
to have it modify the object passed in.
simplify(&fraction);
Similar, but this one takes a double
. I think you'd want it to take a Fraction &
as well.
- Your
entry
andsimplify
functions never get defined, but you still try to use them. display
should take aFraction
in order to print the parts of it.
这篇关于需要帮助理解struct,和程序的一些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!