简体   繁体   English

C ++中的全局变量

[英]Global variables in C++

I am supposed to write a program that should read from input numbers in the main() part, and then make some calculations in other bool functions. 我应该写一个程序,该程序应该从main()部分的输入数字中读取,然后在其他布尔函数中进行一些计算。 I don't want to insert the whole arrays of the numbers and all the other parameters in the functions everytime i call them. 我不想每次调用它们时都将整个数字数组和所有其他参数插入函数中。

My question is this: Can i make somehow in c++ to read input in some variables, but in a way that other functions outside of main() will also "know" this variables and what's inside them so i don't have to put a lot of arguments when i call the functions ? 我的问题是:我可以在c ++中以某种方式读取某些变量的输入,但是以某种方式,main()之外的其他函数也会“知道”该变量及其内部的内容,因此我不必放置我调用函数时有很多参数?

This is the code: 这是代码:

#include <iostream>
using namespace std;

inline bool del(int n)
{
    int i;
    for(i=0;i<s1;i++)
    {
        if((n % a[i]) == 0) return true;
    }
    return false;
}
inline bool ned(int n)
{
    int i;
    for(i=0;i<s2;i++)
    {
        if((n % b[i]) != 0) return true;
    }
    return false;
}
int main(void)
{
    int s1, s2, a[25], b[25];
        int m, n, i, k=0;
    bool d, nd;
    cin >> s1 >> s2 >> m >> n;
    for(i=0;i<s1;i++)
        cin >> a[i];
    for(i=0;i<s2;i++)
        cin >> b[i];
    for(i=m;i<=n;i++)
    {
        d = del(i);
        nd = ned(i);
        if(d == true && nd == true) ++k;
    }
    cout << k << endl;
    return 0;
}

int s1, s2, a[25], b[25] <- These are the vars i need to be seen by the other functions (because i use them as you can see). int s1,s2,a [25],b [25] <-这些是我需要其他功能看到的变量(因为您可以看到它们,因为我使用了它们)。

I tried declaring them like global, but that didn't work, i got errors like "was not declared in this scope" . 我尝试将它们声明为global,但是那行不通,出现了诸如“未在此范围内声明”之类的错误。

Thank you for the help. 感谢您的帮助。

Making variables global for this reason is bad habit. 因此,使变量全局化是一个坏习惯。 Either just pass the arrays to the functions, or make the whole thing into an object and make the arrays and functions members of the class. 要么将数组传递给函数,要么将整个对象变成一个对象,并使数组和函数成为类的成员。 This is what OOP is about. 这就是OOP的目的。

What your looking for is just to put the variables in the global scope. 您要找的只是将变量放入全局范围。 I don't know what you mean about "not declared in this scope" errors, because the following works just fine: 我不知道您对“未在此范围内声明”错误的意思,因为以下方法可以正常工作:

#include <iostream>
using namespace std;

    int s1, s2, a[25], b[25];

inline bool del(int n)
{
int x = s1 + s2;
    int i;
    for(i=0;i<s1;i++)
    {
        if((n % a[i]) == 0) return true;
    }
    return false;
}
inline bool ned(int n)
{
    int i;
    for(i=0;i<s2;i++)
    {
        if((n % b[i]) != 0) return true;
    }
    return false;
}
int main(void)
{
        int m, n, i, k=0;
    bool d, nd;
    cin >> s1 >> s2 >> m >> n;
    for(i=0;i<s1;i++)
        cin >> a[i];
    for(i=0;i<s2;i++)
        cin >> b[i];
    for(i=m;i<=n;i++)
    {
        d = del(i);
        nd = ned(i);
        if(d == true && nd == true) ++k;
    }
    cout << k << endl;
    return 0;
}

As you can see, I've declared them globally and use them in del(). 如您所见,我已经全局声明了它们,并在del()中使用了它们。

However, this is bad practice and you should be using OOP w/ structs or classes to pass and organize your data. 但是,这是不好的做法,您应该使用带有结构或类的OOP来传递和组织数据。

您的声明不是全局声明,只需将a [25]和b [25]的声明移到main之外即可。

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

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