Functions and Arrays help

I keep getting an error saying that "Reference to 'array' is too ambiguous"
Note: I am using Xcode for MacOS
Thanks for any help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE = 5;
int array[SIZE];
int choice;



void arrEnter()
{
    for(int i=0;i<5;++i)
    {
        cin >> array[i];
    }
}

void arrSum()
{
    int sum=0;
    for (int i=0; i<5; ++i)
    {
        sum+=array[i];
    }
    cout << "The sum of the array is " << sum;
}

void meanArr()
{
    int mean=0;
    for (int i =0; i<5; ++i)
    {
        mean+=array[i];
    }
    mean /=5; //we know it will only be out of 5 numbers
    cout << "The mean of this array is " << mean;
}

void displayArr()
{
    for (int i=0; i<5; ++i)
    {
        cout << array[i] << ",";
    }
}

int main()
{
    cout << "Please input values for an array of five integers: ";
    arrEnter();
    
    cout << "Choose what to do with this array: ";
    cout << "1. Sum " << endl;
    cout << "2. Mean " <<endl;
    cout << "3. Display Array " << endl;
    cout << "4. Edit Array " << endl;
    cout << "5.Exit " << endl;
    
    cin >> choice;
    
    switch(choice)
    {
        case 1:
            arrSum();
            break;
        case 2:
            meanArr();
            break;
        case 3:
            displayArr();
            break;
        case 4:
            arrEnter();
            break;
        case 5:
            cout << "Thanks for using this program" ;
            
            
    }
    
    
    
    
    
    system ("read");
    
    
    
    
    return 0;
    
}
The problem is a name collision with something in the standard namespace, thanks to your use of using namespace std;, and the way that the LLVM folks wrote the standard library. Note: they didn't break any rules.

Remove your using-statement and call things by their full name, or qualify array as ::array. This breaks (under at least some versions of the LLVM/Clang compiler) because some part of the standard library that you've included includes a declaration of the name std::array. Since you've imported everything in namespace std into the global namespace, there's a name collision; the compiler cannot determine which "array" you mean.

There has been plenty of stuff written as to why using namespace x is a bad idea, and this is why. You're lucky because your code doesn't compile, but if you weren't, the behavior of your code could silently change. Bugs like that are horrible to diagnose.

P.S.: you might have got better or quicker help if you posted the entire text of the error message, verbatim.
Last edited on
you might have got better or quicker help ...


quicker perhaps but I doubt better ... ;)
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>

using namespace std;

const int SIZE = 5;

void arrEnter(int anArray[])
{
    for(int i=0;i<5;++i)
    {
        cin >> anArray[i];
    }
}

void arrSum(int anArray[])
{
    int sum=0;
    for (int i=0; i<5; ++i)
    {
        sum+=anArray[i];
    }
    cout << "The sum of the array is " << sum;
}

void meanArr(int anArray[])
{
    int mean=0;
    for (int i =0; i<5; ++i)
    {
        mean+=anArray[i];
    }
    mean /=5; //we know it will only be out of 5 numbers
    cout << "The mean of this array is " << mean;
}

void displayArr(int anArray[])
{
    for (int i=0; i<5; ++i)
    {
        cout << anArray[i] << ",";
    }
}

int main()
{
    int array[SIZE];
    int choice;
    
    cout << "Please input values for an array of five integers: ";
    arrEnter(array);
    
    cout << "Choose what to do with this array: ";
    cout << "1. Sum " << endl;
    cout << "2. Mean " <<endl;
    cout << "3. Display Array " << endl;
    cout << "4. Edit Array " << endl;
    cout << "5. Exit " << endl;
    
    cin >> choice;
    
    switch(choice)
    {
        case 1:
            arrSum(array);
            break;
        case 2:
            meanArr(array);
            break;
        case 3:
            displayArr(array);
            break;
        case 4:
            arrEnter(array);
            break;
        case 5:
            cout << "Thanks for using this program\n";
            break;
        default:
            cout << "Bad choice\n";
    }
    
    return 0;
}
Topic archived. No new replies allowed.