如何使用C ++中的函数初始化结构?

时间:2016-09-21 20:13:03

标签: c++ struct

我想制作一个进行分数计算的程序,并且我已经制作了一个具有2个属性的分数结构:分子和分母。我希望能够使用一个名为init(fraction x)的重载函数初始化这些属性。重载如下:

init(fraction x) //Initializes a fraction struct with numerator and denominator equal to 0
init(fraction x, int a) //initializes fraction struct to a/1
init(fraction x, int a, int b) //initializes fraction struct to a/b

然后我尝试测试它并输出一个像这样的分数

int main() {

fraction a;
init(a,1);

cout << "The fraction is: ";
print(a); 
cout << endl;

return 0;
}

但无论如何我每次都得到0/0。我认为这是价值/参考问题的传递,但我不知道如何解决这个问题。这是我的所有代码:

Header.h

#include <iostream>
using namespace std;

struct fraction {
    int numerator = NULL, denominator = NULL;
};

void init(fraction x);                          // set fraction to 0
void init(fraction x, int n);                // set fraction to n/1
void init(fraction x, int a, int b);       // set fraction to a/b
fraction add(fraction a, fraction b);                        // adds a and b and returns the sum (a and b unchanged)
fraction sub(fraction a, fraction b);                        // subtracts a and b and returns difference
fraction mul(fraction a, fraction b);                        // multiplies a and b and returns the product
fraction div(fraction a, fraction b);             // divides a and b and returns the quotient
void print(fraction x);                                    // prints the fraction (no improper fractions)
void read(fraction x);                                    // reads fraction 3 / 5 into x
void reduce(fraction x);                               // simplifies x to lowest common terms
int gcd(int a, int b);                                       // finds greatest common divisor of a and b

Source.cpp

#include "Header.h"

void init(fraction x) {
    x.numerator = 0;
    x.denominator = 0;
}

void init(fraction x, int n) {
    x.numerator = n;
    x.denominator = 1;
}

void init(fraction x, int a, int b) {
    x.numerator = a;
    x.denominator = b;
}


fraction add(fraction a, fraction b) {
    return a;
}


fraction sub(fraction a, fraction b) {
    return a;
}

fraction mul(fraction a, fraction b) {
    return a;
}

fraction div(fraction a, fraction b) {
    return a;
}

void print(fraction x) {
    cout << x.numerator << '/' << x.denominator;
}

void read(fraction x) {

}

void reduce(fraction x) {

}

int gcd(int a, int b) {
    return a;
}

Main.cpp的

#include "Header.h"

int main() {

fraction a;
a.numerator = a.denominator = 1;
init(a,1, 2);

cout << "The fraction is: ";
print(a); 
cout << endl;

return 0;
}

2 个答案:

答案 0 :(得分:4)

  

但无论如何我每次都得到0/0。我认为这是价值/参考问题的过关,但我不确定如何解决这个问题。

这是正确的诊断。您按值传递{{ form.non_field_errors }} {{ form.team1.label_tag }}{{ form.team1 }}{{ form.team1.errors }} {{ form.team2.label_tag }}{{ form.team2 }}{{ form.team2.errors }} 。对副本进行更改,而不是原始对象。将功能更改为:

fraction

void init(fraction& x); void init(fraction& x, int a); void init(fraction& x, int a, int b); read存在同样的问题。将它们更改为:

reduce

答案 1 :(得分:2)

由于您是在C ++中执行此操作,因此最好的方法是为struct创建一个构造函数:

struct fraction
{
    int a_, b_;
    fraction(int a = 0, int b = 1): a_(a), b_(b){}
}

然后你不需要任何其他功能来初始化你的结构,你只需要做

fraction frac{1,2}; // 1/2

实际上,在C ++ 11中你甚至不需要构造函数,你可以只使用类内初始化,如果你想改变值,你依赖于struct是一个汇总,您可以使用aggregate initialization

struct fraction
{
    int a_ = 0, b_ = 1;
};

fraction frac1; // default
fraction frac2{1,2}; // 1/2, aggregate initialization

还要考虑为您的分数重载运算符,例如operator+等,这样您就可以编写适当的C ++代码(而不是C ++中的C语言)。事实上,在学习C ++时,编写fraction类是一种典型的练习。

旁注:你的

int numerator = NULL, denominator = NULL;

恰好编译,因为NULL是一个等于零的编译时常量。但这是非常糟糕的做法。在C ++ 03中为空指针保留NULL,而不是为零初始化变量。 C ++ 11已弃用它,现在使用nullptr,而int不再是$(document).on('click', '.your-element', function (e) { e.preventDefault(); doSomething(); ); ,因此不会发生不想要的转换。