0

I'm encountering an error I've not seen before, that states that reference to things are ambiguous.

I'm writing a small test program that calculates a running median. As the list grows, it recalculates the median. In this case, median means the middle number in the list, (or upper middle). Thus, the median of 7 is 7, the median of 7 and 9 is 9, and the median of 7 3 and 9 is 7.

I'm accomplishing this (I hope) with two dynamic arrays. Initially, the first value is set as the median, and then each number entered is compared to the current median. The median is obtained for calculating the middle element, between two arrays.

The left array is for all values less than the median, and the right array is for all greater. I use insertion sort to order the numbers in each array (it's great at almost sorted lists).

I just don't understand the errors I'm getting and or where I've gone wrong. I'm fairly new to C++, so I've opted for a more simple approach to the issue.

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> left;
vector<int> right;
int leftCount = 0;
int rightCount = 0;
void leftInsertionSort(int);
void rightInsertionSort(int);
void inputNumber(int, int);

int main(int argc, char** argv) {

    int length = 0;
    int value;
    int median;
    string input;

    while (cin >> input) {
        value = atoi(input.c_str());

        inputNumber(value, median);

        if (leftCount > rightCount) {
            median = (((leftCount + rightCount) / 2) + 1);
            cout << left[median];
        } else {
            median = (((leftCount + rightCount) / 2) + 1) - leftCount;
            cout << right[median];
        }
    }

    return 0;
}

void inputNumber(int value, int median) {
    if (leftCount == 0 && rightCount == 0) {
        left[0] = value;
        median = value;
        leftCount++;
    } else
    if (leftCount == 1 && rightCount == 0) {
        right[0] = value;
        if (left[0] > right[0]) {
            right[0] = left[0];
            left[0] = value;
        }
        median = right[0];
        rightCount++;
    } else
    if (value < median) {
        left[leftCount] = value;
    } else {
        right[rightCount] = value;
    }
}

void leftInsertionSort(int lLength)
{
    leftCount++;
    int key, i;
    for(int j = 1; j < lLength; j++)
    {
        key = left[j];
        i = j - 1;
        while (left[i] > key && i >= 0) {
            left[i+1] = left[i];
            i--;
        }
        left[i+1] = key;
    }
}

void rightInsertionSort(int rLength)
{
    rightCount++;
    int key, i;
    for(int j = 1; j < rLength; j++)
    {
        key = right[j];
        i = j - 1;
        while (right[i] > key && i >= 0) {
            right[i+1] = right[i];
            i--;
        }
        right[i+1] = key;
    }
}

The error I seem to be getting is 'error: reference to ‘left’ is ambiguous'

Edge
  • 2,456
  • 6
  • 32
  • 57
  • error: reference to ‘left’ is ambiguous – Edge Jun 10 '12 at 06:48
  • I get the same for right – Edge Jun 10 '12 at 06:50
  • Is thhere any reason you are not using `std::multiset`? This will order the numbers for you. – Troubadour Jun 10 '12 at 07:00
  • I think there is algorithmic error also. The correct algoritm is: 1. insert new element to correct array based on comparison with median; 2. keep arrays almost same size - if one array after insertion is bigger than other in two elements than move corresponded element to another – Eugen Martynov Jun 10 '12 at 07:01

3 Answers3

2

Judging from the compiler error I get when trying to compile that, it seems that the namespace std defines names left and right, which you're also using as variable names. The compiler can't decide which definition to use, so you get the error. It's for reasons like these that importing everything from a namespace is frowned upon - you'd be better off either explicitly importing the names you need or using namespace qualifiers.

In any case, your algorithm seems needlessly complicated. Why not just keep a single vector, push_back into it when you get a new number, place the number at the right index using an insertion algorithm, and then just return the upper middle element of the vector?

Shoaib
  • 561
  • 3
  • 11
1

left and right are flags in iostream.

Just rename the variables.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

This is a good example of why #using namespace std is not a good idea. left and right are also defined for the std namespace and there's now a conflict. If you leave out that line and refer to vector, string, cin and cout by specifying their namespace explicitly with std:: you wouldn't have encountered this conflict.

acraig5075
  • 10,588
  • 3
  • 31
  • 50