数组"not used in this scope" .为什么?

array "not used in this scope". Why?

本文关键字:scope 为什么 this in not used 数组      更新时间:2023-10-16

我收到一个错误,说"邻接矩阵'未在此范围内使用"就在main的末尾(在末尾的函数makebond之前)(注释的第112行"BROKEN LINE")。为什么?很抱歉这很简单。我正在使用 g++ ($ g++ a.c -o f) 进行编译。

这是代码:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define PI 3.1415926535897932384626433832795
#define sqr(x) ((x)*(x))
#define count 500
double density;
double volume;
int N;
double beta = 0.1;
double R = 5;
double rob = 1;
int dimension = 2;
double eps=0.1; // Increase in density
double mindensity = 0; // Minimum density
double maxdensity = 8; // max.dens (scaled for the sake of ensuring int()
int makebond(double x);
int main(){
    srand(time(0));
    for (int rho=mindensity;rho<=(maxdensity/eps);density++){
        N = floor(density*volume);
        double nodepositions[N][dimension];
        // Place nodes in volume (square side L, circle volume *R and obstacle *rob)
        for (int i=0;i<N;i++){
            int L = 5;
            double distancefromorigin;
            double x = (L*(rand()/RAND_MAX))-(L/2);
            double y = (L*(rand()/RAND_MAX))-(L/2);
            distancefromorigin = sqrt((x*x)+(y*y));
                if(distancefromorigin<R){
                    if(distancefromorigin>rob){
                        nodepositions[i][0] = x;
                        nodepositions[i][1] = y;
                    }
                }
        }
        double adjacencymatrix [N][N];
        double itzhak; //distance of node 1 from the centre
        double isaac; //distance of node 2 from the centre
        double vivaldi; //distance between node 1 and node 2
        double phi; // a function of the above 3 doubles (see later usage)
        double rubicon; // maximum distance nodes within the icecream can be apart before becoming visually indepdendent
        double maxtheta; // "in the icecream" means theta < maxtheta
        double theta; // angular displacement of inner point from the line bisecting the icecream
        // Create adjacency matrix (note alternative implementation using incidence lists)
        for (int i=0;i<N;i++){
            for (int j=0;j<N;j++){
                double x0 = nodepositions[i][0];
                double y0 = nodepositions[i][1];
                double x1 = nodepositions[j][0];
                double y1 = nodepositions[j][1];
                itzhak = sqrt(sqr(x0) + sqr(y0));
                isaac = sqrt(sqr(x1) + sqr(y1));
                vivaldi = sqrt(sqr(x0-x1) + sqr(y0-y1));
                phi = ((sqr(vivaldi)+sqr(itzhak)-sqr(isaac))/(2*vivaldi*itzhak));
                rubicon = ((itzhak*phi) - sqrt((sqr(rob)) - ((sqr(itzhak))*(1-sqr(phi)))));
                maxtheta = asin(rob/itzhak);
                theta = acos(phi);
                if (x0==x1 && y0==y1){
                    adjacencymatrix[i][j] = 0;
                }
                else{       
                    if (isaac<itzhak && theta<maxtheta) {           
                        if (vivaldi>rubicon){   
                            adjacencymatrix[i][j] = 0;}
                        else {
                            adjacencymatrix[i][j] = makebond(vivaldi);}             
                    }
                else{adjacencymatrix[i][j] = makebond(vivaldi);}
                }
            }
        }
    }
    FILE *datafc1;
    datafc1 = fopen("matrix.dat", "w");
    for (int ii = 0; ii<N; ii++){
        for (int jj = 0; jj<N; jj++){       
            int aaa;
            aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
            fprintf(datafc1,"%i", aaa);
        }
    }
    fclose(datafc1);
    return 0;
}
/////////////////////////////
////////////////
///////  --End Main--
////////////////
////////////////////////////
int makebond(double x){
    // This function takes in the euc. dist. between two nodes and draws a link with prob. H(r)
    double randomnumber = (rand()/RAND_MAX); // Random number between 0 and 1
    double hr = exp(-beta*sqr(x));// ***Connection function***
    int a = 1; // Number to be put into adjacency matrix
    if (randomnumber > hr){
        a = 0;
    }
    return a; //Returns 0 or 1 depending on prob. dist.
}
adjacencymatrix 是在

第一个for循环中声明的,因此在底部的打印输出循环中,在您使用它的最后一个位置之前,它超出了范围。

此外,您还有一条无用的using namespace std;线。 代码不包含任何包含std命名空间符号的标头。

第 57 行中的代码:

    double adjacencymatrix [N][N];

在 for 循环内,在该循环外,adjacencymatrix未定义。

U 矩阵在第 11 行的 for 循环中定义。因此,它超出了第 112 行的范围。

FILE *datafc1;
datafc1 = fopen("matrix.dat", "w");
for (int ii = 0; ii<N; ii++){
    for (int jj = 0; jj<N; jj++){
        int aaa;
        //error adjacencymatrix is declared in your first for loop
        aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
        fprintf(datafc1,"%i", aaa);
    }
}