0

I wrote a basic get function that takes the elements of a struct, and copies/stores them into separate variables. I want to do the same thing, except to store an array of structs into array variables (the key thing is that I want to use the get function that I already wrote). I can easily do what I want to do without embedding my first function, but I am required to use my first function in the second, and am having some problems.

One of my errors is that there isn't a matching function call in the embedded get function that I put inside my second function (I thought that I matched up the variables correctly with my first get function (i.e. n, c, a, and x)).

Thanks for looking.

#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; 
} 
2
  • Some indentation would be nice. Commented Feb 23, 2014 at 21:32
  • The title reminds me of XY-problems immediately. So: Why? Commented Feb 23, 2014 at 21:48

2 Answers 2

1

You probably wanted to say

get(n[i], c[i], a[i], x[i]); 

This is not answering your XY-problem though

"fixed" code (to compile) Live On Coliru

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Don't know why I forgot to just add the index to each of the parameters. Lots of HW, so it probably just slipped the mind.
1

It seems to me that in your get functions with the arrays you're always storing the values into the same place in each array. You should probably be doing something like:

get(n[i], c[i], a[i], x[i] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.