views:

105

answers:

4

Hello, I wounder how i can make this code work?

#include <iostream>
using namespace std;

void writeTable(int (&tab)[],int x){
    for(int i=0;i<x;i++){
        cout << "Enter value " << i+1 <<endl;
        cin >> tab[i] ;
    }
}


int main(void){
    int howMany;
    cout << "How many elemets" << endl;
    cin >> howMany;

    int table[howMany];
    int (&ref)[howMany]=table;
    writeTable(ref,howMany);
    return 0;
}

And here are the errors that I have:

|4|error: parameter ‘tab’ includes reference to array of unknown bound ‘int []’|
|18|error: invalid initialization of reference of type ‘int (&)[]’ from expression of type ‘int [(((unsigned int)(((int)howMany) + -0x00000000000000001)) + 1)]’|
|4|error: in passing argument 1 of ‘void writeTable(int (&)[], int)’|

Thanks for help

+7  A: 

If you are intending to pass the size of the array, then remove the reference

void f(int a[])

is equivalent to

void f(int* a)

so no copying will be done, if that is the concern.

If you want to take an array by reference, then you MUST specify the dimension. e.g.

void f(int (&a)[10])

Naturally, the best of the two is the third solution, which is to use std::vector's and pass them by reference, reference to const or by value if needed. HTH

Armen Tsirunyan
Agree, passing in a pointer to the array is the way to go here.
JeffW
Ok thx for help but i have one additional question in the third option I can pass dimension using a variable? If so how would look calling that function and it's prototype?
Artur
@Artur: See Prasson Saurav's answer and my comment to it. Or do you mean the vector's? For vector's see Amardeep's answer :)
Armen Tsirunyan
@Artur - Why do you want to take the array by reference? What are you trying to achieve here versus the simpler way of passing in the pointer?
JeffW
I must do this by using references I can't user any other technique such vectors etc now I know how to do this more less. Thx for help and explanations ;)
Artur
+2  A: 

Arrays of references are illegal, if that is what you are trying to do. It's not 100% clear to me from the title.

Steve Townsend
virtual -1: Obviously he is trying to create a reference to an array, not an array of references
Armen Tsirunyan
@Armen - the syntax is confused enough that I respectfully disagree. It's likely that your answer is what he needs though.
Steve Townsend
@Steve: If you look at the code(let alone the syntax), it is VERY obvious that he was not trying to create an array of references
Armen Tsirunyan
@Armen - the older I get, the less there is that is VERY obvious to me
Steve Townsend
@Steve: My youth is my enemy :)
Armen Tsirunyan
@Armen - more of an asset than an enemy, I am sure :-)
Steve Townsend
+6  A: 

You need not specify the dimension of the array if you make writeTable a function template.

template <typename T,size_t N>
void writeTable(T (&tab)[N]) //Template argument deduction
{
    for(int i=0 ; i<N ; i++){
       // code ....
    }
}

.

int table[howMany]; // C++ doesn't have Variable Length Arrays. `howMany` must be a constant
writeTable(table);  // type and size of `table` is automatically deduced
Prasoon Saurav
but take heed that in this case you will not be able to pass dynamically allocated arrays to this function.
Armen Tsirunyan
@Armen : Yes! Why would he need a dynamically allocated array anyway (he would use `std::vector`)?
Prasoon Saurav
@Prasoon: My comment was intended for the OP, not you :)
Armen Tsirunyan
+4  A: 

Here is a slightly more C++ style of doing it:

#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (unsigned int i=0; i<tab.size(); i++)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            tab[i] = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}
Amardeep
+1, but a more standard/portable way is to declare i's type as vector<int>::size_type
Armen Tsirunyan
@Armen: I usually use size_t but laziness set in. I wasn't aware of the vector<>::size_type. Thanks for the info.
Amardeep
@Amardeep: Well, USUALLY vector<T>::size_type is typedef'd as size_t, which in turn is USUALLY typedef'd as unsigned int. But there are no guarantees
Armen Tsirunyan
@Amardeep - +1 - you could also show the `vector<int>::iterator` alternative vs `tab[i]`
Steve Townsend
@Steve: The only reason I kept the numeric loop counter was because the original functionality displayed the index number with each read. Otherwise a for_each or while would have been nicer. Thanks.
Amardeep
@Amardeep - sure, sometimes a direct analog to the original code is helpful.
Steve Townsend