Visual Studio为无真实原因提供了模糊错误

时间:2015-02-21 01:31:18

标签: c++ visual-studio

它出现以下错误:

error C2872: 'count' : ambiguous symbol

Count变量已声明为全局变量,代码在Sublime Text中编译并运行。不明白为什么Visual Studio会为它哭泣。

#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;
}

2 个答案:

答案 0 :(得分:3)

使用命名空间std是你的问题。

看起来iostream或fstream标头的Microsoft实现本身包含算法。这导致名称与std :: count()冲突。

答案 1 :(得分:0)

所以,正如@Retiredninja建议的那样,如果我选择将while(!in.eof())替换为while(in >> a >> b)

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

while循环中的其余代码是否保持不变或是否意味着,在while(in >> a >> b)中检查条件时,输入是否已被读入a和b?

并成为以下内容:

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

还是留下来:

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