结构格式问题,无法转换
Structure formatting issue, cannot convert
我在编译时不断收到同样的错误。简单的背景,这个程序从用户那里获取两个整数(比如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++,因为m
和n
不是编译时常数。它被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;
}
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 将CHW格式的浮点向量转换为cv::Mat
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- 如何在C++中将 chrono::秒转换为 HH:MM:SS 格式的字符串?
- 以C++格式将字符串转换为数字
- 将 BGR 图像转换为 jpeg 格式的 base64 字符串
- 将'\x00\x00\x00'格式的字符串转换为无符号字符数组
- 如何在C++中将"%Y%m%d"格式字符串转换为time_t变量?
- 如何将小端格式的QByteArray转换为无符号长整型
- 将毫秒转换为给定格式的持续时间
- 如何将 2d 数组转换为 svg 或 png 格式的数独图像?
- 时间转换为 24 小时格式 cpp
- 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的time_t格式?
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 如何在OpenCV中将4维矩阵转换为NHWC格式
- C++ 将字符串数据包转换为 iphdr语言 - 字符串数据包的格式应该是什么?
- 1 字节整数不转换 I/O 格式
- time_t转换格式问题