【问题标题】:My functions are not updating global variables / the variables used in main我的函数没有更新全局变量/ main 中使用的变量
【发布时间】:2020-06-13 01:07:44
【问题描述】:

我的项目是一些简单的矩阵运算,它应该可以正常工作,但我的输入函数似乎没有更新全局变量,它们可能是我正在尝试做的错误的数据类型,但我不确定。这是应该发生的事情的链接http://cpp.sh/22kie。 这里还有一个链接到所有带有 Makefile https://drive.google.com/open?id=1tmIbdEWXJJ54moQKy8rkZODFkXKDU2Nz

这是我的头文件:

#ifndef project1
#define project1

static int choice, rows1, cols1, rows2, cols2, k;
static char choice2;
static int *mat1 = new int[10 * 10];
static int *mat2 = new int[10 * 10];
static int *matf = new int[10 * 10];

void output(int *matf, int rows, int cols);

void addition(int *mat1, int *mat2, int *matf, int rows, int cols);

void subtraction(int *mat1, int *mat2, int *matf, int rows, int cols);

void multiplication(int *mat1, int *mat2, int *matf, int rows1, int cols1, int rows2, int cols2);

int input();
#endif

这是:我的主要内容:

#include <string>
#include "project1.h"
using namespace std;

int main()
{

    input();

    switch (choice)
    { //this switch block will call the correct fucntion with respect to the user's choice

    case 1:
        addition(mat1, mat2, matf, rows1, cols1);

        break;

    case 2:
        subtraction(mat1, mat2, matf, rows1, cols1);

        break;

    case 3:
        multiplication(mat1, mat2, matf, rows1, cols1, rows2, cols2);

        break;

    case 0:
        return 0;
    default:
        cout << "invalid 2input";
    }
    //the end of the main function will repeat if the user would like

    cout << "\n Would you like to do another operation y/n?" << endl;
    cin >> choice2;

    if (choice2 == 'y')
    {
        main();
    }
    else
    {
        return 0;
    }
}

这是输入函数:

int input()

{

    //input function

    cout << "Menu"
         << "\n 1. Addition"
         << "\n 2. Subtraction"
         << "\n 3. Multiplication"
         << "\n 0. Exit"
         << "\n Enter the number of your choice" << endl;
    cin >> choice;

    if (choice == 0)
    { // this will kill the main function if "Exit" is chosen, or repeat if an invalid number is chosen
        return 0;
    }
    else if (choice > 3)
    {
        cout << "invalid input" << endl;
        input();
    }
    else
    {
    }
    //this part of the code will prompt the user for the rows and columns for each matrix
    cout << "Number of rows for the first Matrix? (max 10)" << endl;
    cin >> rows1;

    cout << "Number of columns for the first Matrix? (max 10)" << endl;
    cin >> cols1;

    cout << "Number of rows for the second Matrix? (max 10)" << endl;
    cin >> rows2;

    cout << "Number of columns for the second Matrix? (max 10)" << endl;
    cin >> cols2;

    //this if/else statement checks that the matrices are the right size to be operated
    if ((choice == 1 || choice == 2) && ((cols1 != cols2) || (rows1 != rows2)))
    {
        cout << " error: for addition and subraction the two matrices must have the same amount of elements";
        choice = 0;
        return 0;
    }
    else if ((choice == 3) && (rows2 != cols1))
    {
        cout << " error: for multiplication the columns of the first must equal the rows of the second";
        choice = 0;
        return 0;
    }
    else
    {
    }

    // this for loop allows the user to input each element for the first matrix
    cout << "input the elements of the first matrix: " << endl;

    for (int i = 0; i < rows1; i++)
    {
        for (int j = 0; j < cols1; j++)
        {
            cout << "Enter element "
                 << "(" << i << "," << j << "): ";
            cin >> k;
            *(mat1 + i * cols1 + j) = k;
        }
    }

    output(mat1, rows1, cols1); //this call to output will allow the user to see what they have imputed

    // this for loop allows the user to input each element for the second matrix
    cout << "input the elements of the second matrix: " << endl;
    for (int i = 0; i < rows2; i++)
    {
        for (int j = 0; j < cols2; j++)
        {
            cout << "Enter element "
                 << "(" << i << "," << j << "): ";
            cin >> k;
            *(mat2 + i * cols2 + j) = k;
        }
    }

    output(mat2, rows2, cols2); //this call to output will allow the user to see what they have imputed
}```

编辑:将所有内容切换为局部变量后的新 main:

主要:

#include <iostream>
#include <string>
#include "project1.h"
using namespace std;

int choice, rows1, cols1, rows2, cols2, k;
char choice2;
int *mat1 = new int[10 * 10];
int *mat2 = new int[10 * 10];
int *matf = new int[10 * 10];

int main()
{


    input( choice, rows1, cols1, rows2, cols2, k, mat1, mat2, matf);
cout<<rows1;
    switch (choice)
    { //this switch block will call the correct fucntion with respect to the user's choice

    case 1:
        addition(mat1, mat2, matf, rows1, cols1);

        break;

    case 2:
        subtraction(mat1, mat2, matf, rows1, cols1);

        break;

    case 3:
        multiplication(mat1, mat2, matf, rows1, cols1, rows2, cols2);

        break;

    case 0:
        return 0;
    default:
        cout << "invalid input";
    }
    //the end of the main fucntion will repeat if the user would like

    cout << "\n Would you like to do another operation y/n?" << endl;
    cin >> choice2;

    if (choice2 == 'y')
    {
        main();
    }
    else
    {
        return 0;
    }
}

标题:

#ifndef project1
#define project1


int output(int *matf, int rows1, int cols1);

int addition(int *mat1, int *mat2, int *matf, int rows1, int cols1);

int subtraction(int *mat1, int *mat2, int *matf, int rows1, int cols1);

int multiplication(int *mat1, int *mat2, int *matf, int rows1, int cols1, int rows2, int cols2);

int input(int choice,int rows1,int cols1,int rows2,int cols2,int k, int *mat1, int*mat2, int*matf);
#endif

【问题讨论】:

  • 1.你怎么知道你的全局变量没有被更新?要验证这一点,请使用您的调试器!!! 2. 全局变量是邪恶的,尽可能避免使用它们(大多数时候)。
  • 我在调用输入后输入cout&lt;&lt;rows1 进行了测试,它输出了0
  • 你的main函数和input函数是在不同的编译单元(.cpp文件)吗?
  • 是的,我的头文件有一个定义文件
  • 如果您有一个main.cpp 和一个input.cpp,并且它们都是#include "project1.h",那么您在.h 文件中声明的每个静态变量都会有两个实例。为避免这种情况,请在.h 文件中使用extern 而不是static,并仅在.cpp 文件之一(可能在main.cpp)中初始化变量。更好的是 - 不要使用全局变量!!!

标签: c++ function types header global-variables


【解决方案1】:

您的变量仍然是全局变量(在函数外部声明)。您应该在 main 中声明它们(尽管这不是导致问题的原因)。

现在的问题是您将值按值传递给input。这意味着,input 被调用,堆栈增加,您发送的值被复制到input 的堆栈空间中。当您在input 中修改它们时,它们会在input 的堆栈上进行修改。 input 然后返回,这些更新的值被删除。您需要将 references 发送到这些变量。这意味着变量的地址将被复制到input的堆栈中,input将使用这些地址回到main函数中的变量并直接更新这些值。

将输入的函数签名更改为

void input(int& choice, int& rows1, int& cols1, int& rows2, int& cols2, int& k, int *mat1, int* mat2, int* matf);

注意input的返回值从int变为void。这是因为当你在main 中调用它时,你没有将input 的返回值分配给任何东西,所以我认为你不需要它。 input 中的任何 return 语句都需要更改为 return;(而不是 return 0; 之类的东西)。

另请注意,在发送到输入的前 6 个参数之后现在有与号 (&amp;)。这样,input 就会有他们的地址并且可以直接更改它们。 mat1mat2matf 已作为指针传递,因此无需更改。

What's the difference between passing by reference vs. passing by value?

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 2019-04-01
    • 2011-12-08
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多