0

It gives the following error:

error C2872: 'count' : ambiguous symbol

Count variable has been declared as a global variable and the code compiles and runs in Sublime Text. Don't understand why Visual Studio is crying over it.

#include <iostream>
#include <fstream>

using namespace std;

int** am; // Adjacency matrix
int* ar, * ar2; // Arrays to work with
int n; // Number of nodes
int node1, node2, k; // For reading input from the console
int count;

bool checkReachability(int, int, int);
void fillArray(int);
void updateArray(int,int);
void freeMemory();

int main() {

    ifstream in;
    in.open("Input2.txt");
    int a, b;
    if(in.is_open()) {
        in >> n;

        // Allocate memory on the heap dynamically for the adjacency matrix:
        am = new int*[n];
        ar = new int[n];
        ar2 = new int[n];
        for (int i = 0; i < n; i++) {
            am[i] = new int[n];
        }

        // Initialize the values of the adjacency matrix with 0s and the principle diagonal with 1s initially:
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    am[i][j] = 1;
                } else {
                    am[i][j] = 0;
                }
            }
        }

        while(!in.eof()) {
            in >> a >> b;
            am[a-1][b-1] = 1;
        }

        cout << "The adjacency matrix input is as follows: \n\n";
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << am[i][j] << "  ";   
            }
            cout << endl << endl;
        }
        in.close();
    } else {
        cout << "\nError reading the input file\n\n";
    }


    char c;
    do {
        cout << "\nPlease enter the input (node1, node2, k): \n";
        cin >> node1 >> node2 >> k;
        fillArray(node1-1);
        count = 0;
        if(checkReachability(node1-1,node2-1,k)) {
            cout << "\nReachable within " << k <<  " steps";
            if (count < k) {
                cout << " (actually " << count << ")";
            }
            cout << endl << endl;
        } else {
            cout << "\nNot reachable within " << k << " steps  \n";
        }
        cout << "\nDo you want to continue? Y/N \n\n";
        cin >> c;
    } while (c == 'Y' || c == 'y');
    freeMemory();
    system("pause");
    return 0;
}


bool checkReachability(int n1, int n2, int k) {
    if (n1 == n2) return true;
    count++;
    if (count <= k) { 
        if (ar[n2] != 0) return true; 
        int x;
        for (int i = 0; i < n; i++) {
            if (ar[i] != 0 && i != n1) {
                ar[i]++;
                x = ar[i];
            }
        }
        for(int i = 0; i < n; i++) {
            ar2[i] = ar[i];
        }
        for(int i = 0; i < n; i++) {
            if (ar2[i] == x) {
                fillArray(i);
                updateArray(x,i);
                if (checkReachability(ar2[i], n2, k)) return true;
            }
        }
    }
    return false;
}


void fillArray(int x) {
    // To fill the array with the adjacencies of a particular node
    for(int i = 0; i < n; i++) {
        ar[i] = am[x][i];
    }
}


void updateArray(int x, int y) {
    for(int i = 0; i < n; i++) {
        if (ar[i] == 1 && i != y) {
            ar[i] = x;
        }
    }
}


void freeMemory() {
    // To free the dynamically allocated memory on the heap
    for (int i = 0; i < n; i++) {
        delete [] am[i];
    }
    delete [] ar;
    delete [] ar2;
}
Retired Ninja
  • 4,343
  • 3
  • 21
  • 35
Southee Rocks
  • 111
  • 1
  • 2
  • 7

2 Answers2

4

using namespace std is your problem.

Looks like the Microsoft implementation of either the iostream or fstream headers themselves include algorithm. This is causing the name clash with std::count().

rmccabe3701
  • 1,278
  • 11
  • 31
0

So, as @Retiredninja suggested, if I choose to replace while(!in.eof()) with while(in >> a >> b) in:

while(!in.eof()) {
      in >> a >> b;
      am[a-1][b-1] = 1;
}

Does the rest of the code in the while loop remain the same or does it mean, the input has already been read into a and b when the condition is checked in while(in >> a >> b)?

And become the following:

while(in >> a >> b) {
    am[a-1][b-1] = 1;
}

or does it remain:

while(in >> a >> b) {
      in >> a >> b;
      am[a-1][b-1] = 1;
}
Southee Rocks
  • 111
  • 1
  • 2
  • 7
  • This is a separate question and probably deserves its own post. That being said: the second option is probably not want you want because every iteration of the loop you will read the values of a and b twice. This will result in you reading past the end of the file. One more suggestion: I noticed you are allocating memory by hand. This is discouraged -- consider using std::vector or something similar – rmccabe3701 Feb 21 '15 at 05:12
  • Oh, vector...yeah. Good idea. Yeah, I didn't really want to allocate it dynamically but since the size of the array needs to be known at compile time, I did what I did, but yeah, I completely overlooked vector. Thanks. – Southee Rocks Feb 21 '15 at 05:20