【发布时间】:2018-06-30 17:10:47
【问题描述】:
我必须为这个项目使用动态数组,但我不能通过引用传递它,我不知道该怎么做,我初始化了二维动态数组,但不知道如何通过引用传递它。
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
#define row 5
#define col 14
typedef string* StrArrPtr;
void set_up_array(string (&&layout_array)[row][col]);
int main()
{
StrArrPtr *layout_array = new StrArrPtr[col];
for(int i = 0; i < row; i++)
{
layout_array[i] = new string[col];
}
//string layout_array[row][col];
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(string (&&layout_array)[row][col])
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}
我是 C++ 新手,所以解决方案可能非常明显,但我就是看不到。任何帮助将不胜感激。
【问题讨论】:
-
您需要使用一个
&,而不是两个。void set_up_array(string (&layout_array)[row][col]); -
R Sahu 它给了我一个错误“错误:没有匹配函数调用'set_up_array'”
标签: c++ multidimensional-array pass-by-reference dynamic-arrays