c++ - 需要帮助理解结构,以及一些程序错误

标签 c++ struct fractions

我不知道如何正确使用结构来实现计算分数的目标(这是必需的)。坦率地说,我不太了解自己在做什么,这只是我的 C++ 类(class)的第三堂课,我感到迷茫……这是分配给我们的任务

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.

这是我的程序,只有 ma​​in 除了“cin”和“cout”,GCF 功能由教授提供,我试图自己做的主要之外的所有其他功能和结构......

 #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;
}

这些是我的错误:

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

对于这篇很长的帖子,我深表歉意,但非常感谢您提供的所有帮助。 如果有人能给我指点一本有用的 C++ 书,我可以在家里或在讲座中阅读(由于语言障碍,我无法很好地理解我的教授),我也会很感激

最佳答案

让我们来看看这些:

error: aggregate 'Fraction fraction' has incomplete type and cannot be defined

现在,在 main() 中,您说了 struct Fraction fraction;。此时,您正在转发声明您的结构。它不完整,因此您不能像它一样使用它。

您应该在 main() 之前定义整个 Fraction 结构。另请注意,struct Fraction fraction; 中的 struct 是不必要的,它是 C 遗留下来的。

 error: 'enter' was not declared in this scope

简单。您已在顶部声明了 entry(),但您正在尝试使用 enter()。不多说了。

 At global scope: w2.cpp:35: error: function definition does not declare parameters

现在这有点令人困惑。这是有问题的行:

struct Fraction fraction{

编译器如何看待它是一个返回 Fraction 的函数,但它缺少参数列表。我不太确定你想用这段代码做什么。

error: 'fraction' was not declared in this scope

看起来您正在尝试使用在其他地方声明的对象。如果您想要 main() 中的那个,则必须将其作为参数传入。如果你想要一个全局变量fraction,你在全局空间中所需要的就是:

Fraction fraction;

这应该发生在 Fraction 结构之后。另请注意,因为 this 与 main() 中的对象名称相同,所以 main() 中的对象会隐藏此对象,如果您想访问全局来自 main() 的一个你需要使用 ::fraction

我希望这有助于澄清一些理解。

我看到的其他一些错误是:

enter(&fraction);

您正在将一个 Fraction * 传递给一个接受两个 int 的函数。我认为您希望这个采用 Fraction &。然后你可以像 enter (fraction); 那样调用它来让它修改传入的对象。

simplify(&fraction);

类似,但是这个需要一个double。我认为您也希望它采用 Fraction &

  • 您的entrysimplify 函数从未被定义,但您仍然尝试使用它们。
  • display 应该采用 Fraction 来打印它的各个部分。

关于c++ - 需要帮助理解结构,以及一些程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809557/

相关文章:

c++ - 减小 .rodata 的大小

c++ - 如何设置我的 Xcode 项目的资源位置

java - 使用数组计算时如何格式化输出?

c++ - 如何使 perf_event_open() 中的 PERF_COUNT_SW_CONTEXT_SWITCHES 配置起作用?

c++ - ERROR LNK1104 - .`obj` 文件没有任何文件名

ios - Swift:在 Struct 中声明委托(delegate)

c - 将指针改组为指向结构的指针

c - 从文件中读取数据到C中的结构

python - 如何简化一个复杂的比率?

c++ - 从 C++ 中的 double 中检索分数部分