我寫了一個基本的get函數,它接受一個結構的元素,並將它們複製/存儲到單獨的變量中。我想做同樣的事情,除了將一個結構數組存儲到數組變量中(關鍵是我想使用我已經寫過的get函數)。我可以輕鬆地做我想做的事情,而不用嵌入我的第一個函數,但是我需要在第二個函數中使用我的第一個函數,並且遇到一些問題。C++:我想將結構中的數據複製/存儲到各個數組中
我的錯誤之一是嵌入式獲取函數中沒有匹配的函數調用,我將其放入第二個函數中(我認爲我正確地將變量與我的第一個get函數(即n,c ,a和x))。
感謝您的期待。
#include <iostream>
#include <cstdlib>
using namespace std;
struct ABC
{
int n;
char c;
double a[3];
};
void get(int & n, char & c, double a[3], const ABC & x)
{
n = x.n;
c = x.c;
for (int i = 0; i < 3; i++){
a[i] = x.a[i];
}
}
void get(int n[], char c[], double a[][3], const ABC x[], int elements)
{
for (int i = 0; i < elements; i++){
get(n, c, a, x);
}
}
int main()
{
ABC x = {number, m, {1, 2, 3}};
ABC xx[4] = {
{123, 'A', {1.1, 1.2, 1.3}},
{234, 'B', {2.1, 2.2, 2.3}},
{345, 'C', {3.1, 3.2, 3.3}},
{456, 'D', {4.1, 4.2, 4.3}}
};
int n;
char c;
int a[] = {};
int n1 [4] = {};
char c1 [4] = {};
double a3 [4] [3] = {};
int elements = 3;
get(n1, c1, a3, xx, elements);
return 0;
}
某些縮進會很好。 – jia103
標題立即讓我想起[XY-問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。所以爲什麼? – sehe