繁体   English   中英

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

[英]How to initialize a struct with a function in C++?

我想制作一个进行分数计算的程序,并制作了一个具有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。 我认为这是一个按价值/参考传递的问题,但我不确定如何解决此问题。 这是我所有的代码:

标头

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

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

那是正确的诊断。 您正在按值传递fraction 更改是对副本而不是原始对象进行的。 将功能更改为:

void init(fraction& x);
void init(fraction& x, int a);
void init(fraction& x, int a, int b);

您对readreduce有同样的问题。 将它们更改为:

void read(fraction& x);
void reduce(fraction& x);

由于您使用C ++进行此操作,因此最好的方法是为struct构造一个构造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是聚合的事实,并且可以使用聚合初始化

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

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

还要考虑重载运算符,例如您的分数中的operator+等,因此您可以编写适当的C ++代码(而不是C ++中的C)。 实际上,编写fraction类是学习C ++时的典型练习。

旁注:您的

int numerator = NULL, denominator = NULL;

碰巧会进行编译,因为NULL是等于零的编译时常量。 但这是非常糟糕的做法。 NULL保留用于C ++ 03中的空指针,而不用于初始化具有零的变量。 C ++ 11弃用了它,现在使用了nullptr ,它不再是int了,因此不会发生不需要的转换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM