结构格式问题,无法转换

Structure formatting issue, cannot convert

本文关键字:转换 格式 问题 结构      更新时间:2023-10-16

我在编译时不断收到同样的错误。简单的背景,这个程序从用户那里获取两个整数(比如m,n),创建一个二维数组(有m行n列),然后计算并打印这些值的乘法和除法表。

我真的很抱歉,因为我的睡眠(和咖啡)有点不足。自从我上次写代码以来,已经两年多了,请尽可能地友善。

我收到的错误如下:

test1.cpp: In function ‘void createTable(int, int)’:
test1.cpp:69: error: cannot convert ‘mult_div_values (*)[(((long unsigned int)(((long int)n) + -0x00000000000000001)) + 1)]’ to ‘mult_div_values**’ for argument ‘1’ to ‘void multArray(mult_div_values**, int, int)’
test1.cpp:70: error: cannot convert ‘mult_div_values (*)[(((long unsigned int)(((long int)n) + -0x00000000000000001)) + 1)]’ to ‘mult_div_values**’ for argument ‘1’ to ‘void divArray(mult_div_values**, int, int)’

很明显,问题在于这个功能,但我不知道问题是什么,如果有人能给我一个提示,告诉我我在寻找什么,我会非常感激。

void createTable(int m, int n)
{
    struct mult_div_values table [m][n]; //Initialize table array
    multArray(table, m, n);
    divArray(table, m, n);
    return;
}

整个代码(如果我的代码让你的专业人士感到尴尬,我深表歉意):

#include <iostream>
#include <string>
#include <stdlib.h> //atoi function
#include <cctype>
using namespace std;
//Declare structure for multiplication and division values
struct mult_div_values {
    int mult;
    float div;
};
//Prototype functions
void multArray(mult_div_values** table, int m, int n);
void divArray(mult_div_values** table, int m, int n);
void createTable(int m, int n);
void checkValues(char* argv[]);
void printMult(mult_div_values** table, int m, int n);
void printdiv(mult_div_values** table, int m, int n);
int main(int argc, char* argv[])
{    
    checkValues(argv); //Passes argument values to check for validity.
    return 0;
}
/*********************************************************************
 ** Parameters: Takes in the 1st and 2nd character arguments from the command line.
 ** Description: Checks that arguments are in fact numbers.  Converts value from character to integer.
 ** Return value: Converted integer values.
 *********************************************************************/
void checkValues(char* argv[])
{   
    //Convert char to int
    int m = atoi(argv[1]);
    int n = atoi(argv[2]);
    //Check that in is 
    if ((m < 1) || (n < 1))
    {
       cout << "Error!" << endl;
    }
    cout << m << n << endl;
    createTable(m, n);
}
/*********************************************************************
 ** Parameters: Takes in column and row values.
 ** Description: Initializes table array.
 *********************************************************************/
void createTable(int m, int n)
{
    struct mult_div_values table [m][n]; //Initialize table array
    multArray(table, m, n);
    divArray(table, m, n);
    return;
}
/*********************************************************************
 ** Parameters: Takes in table array and number of columns/rows.
 ** Description: Computes multiplication values.
 *********************************************************************/
void multArray(mult_div_values** table, int m, int n)
{
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            table[i - 1][j - 1].mult = i*j;
        }
    }
    return;
}
/*********************************************************************
 ** Parameters: Takes in table array and number of columns/rows.
 ** Description: Computes division values.
 *********************************************************************/
void divArray(mult_div_values** table, int m, int n)
{
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            table[i - 1][j - 1].div = i/j;
        }
    }
    return;
}
/*********************************************************************
 ** Parameters: Takes in multiplication values and number of columns/rows.
 ** Description: Builds multiplication table and prints.
 *********************************************************************/
void printMult(mult_div_values** table, int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << table[i][j].mult << "t";
        }
        cout << endl;
    }
    return;
}
/*********************************************************************
 ** Parameters: Takes in division values and number of columns/rows.
 ** Description: Builds division table and prints.
 *********************************************************************/
void printdiv(mult_div_values** table, int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << table[i][j].div << "t";
        }
        cout << endl;
    }
    return;
}

二维数组(矩阵)不是指针数组(形式参数)。

还要注意,这个代码,

struct mult_div_values table [m][n];

不是有效的C++,因为mn不是编译时常数。它被g++(可能也被clang)接受为语言扩展。您最好通过指定-std=c++11-pedantic来关闭它。

还要注意,它是C风格,而不是C++。

在C++中,您不需要在那里重复struct关键字。

在C++中实现这个可变大小矩阵的一个好方法是使用std::vector,每个项使用std::vector,或者使用映射到1D索引的2D索引。你可以很容易地在课堂上把它总结起来。

void createTable(int m, int n)
{
//Initialize table array
mult_div_values** table = new mult_div_values*[n]; 
for(int i = 0; i < n ; i++){
    table[i] = new mult_div_values[m];
}
//Call mult and div funtions
multArray(table, m, n);
divArray(table, m, n);
delete [] table; //Clear allocation
return;
}