2

我知道这可能是一个非常简单的解决方法,但是我很难找到我的错误是什么,而且我在网上查看过的所有帖子都无法帮助我。我在 cout 线上得到错误。这是代码:

#include <iostream>

bool online(int a, int network[a][a]) {
    /*post condition: returns true if every switch in a network is of even degree. Otherwise, returns false.*/
    int switches;
    for(int x=0; x < a; x++) {
        switches = 0;
        for(int y=0; y < a; y++)
            if(network[x][y])
                switches += 1;
        if(switches & 1)
            return 0;
    }
    return 1;
}

int main() {
    int arrayOne[6][6] =
    {
        {0,1,1,0,0,0},
        {1,0,0,1,0,0},
        {1,0,0,1,0,0},
        {0,1,1,0,1,1},
        {0,0,0,1,0,1},
        {0,0,0,1,1,0}
    };
    int arrayTwo[8][8] =
    {
        {0,1,1,0,0,0,0,0},
        {1,0,0,1,0,0,0,0},
        {1,0,0,1,0,0,0,0},
        {0,1,1,0,1,0,0,0},
        {0,0,0,1,0,1,1,0},
        {0,0,0,0,1,0,0,1},
        {0,0,0,0,1,0,0,1},
        {0,0,0,0,0,1,1,0}
    };
    std::cout << online(6, arrayOne) << std::endl;
    std::cout << online(8, arrayTwo) << std::endl;
}    
4

1 回答 1

4

对于 C99(支持可变长度数组),没有<iostream>. 确保将程序编译为 C 而不是 C++,然后改用<stdio.h>包含<stdbool.h>.

printf("%d\n", online(6, arrayOne));
printf("%d\n", online(8, arrayTwo));

对于 C++,您的编译器可能不支持可变长度数组。您应该使用模板代替online().

template <unsigned a>
bool online(int, int (&network)[a][a]) {
    //...
}
于 2013-06-29T07:09:03.273 回答