Home > Back-end >  Function not declared in scope even after declaring it before int main in C
Function not declared in scope even after declaring it before int main in C

Time:04-20

I have created two vectors, x_listand y_list. I have two functions declared before int main named x_i and y_j, which are used to extract element one before the value is called. Eg If x_i(2) is called x_list[1] would be the answer same for y_j. I have read a few answers about this problem, saying that variables should be inside the scope, but I could find that problem in my code.

#include<iostream>
#include<iomanip>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

void display(vector<double> &v){
    for (int i = 0; i < v.size(); i  )
    {
        cout<<v[i]<<" ";
    }
    cout<<endl;
}
//-------------------------------
double x_i(int node_number)
{
    x_list[node_number-1];
}
//------------------------------
double y_j(int node_number)
{
    y_list[node_number-1];
}
//-----------------------------
int main(){

    const int N = 401;
    const double pi = 3.141592653589793;
    double L = pi/2;
    double r = 1.5;
    //-------------------------------------------------------------------
    vector<double> x_list_0_to_pidiv2;
    double val;
    for (int i = 0; i < (int(N/4) 1); i  )
    {
        val = ((L/2)*((1-(tanh(r*(1-(2*((i*(1))/int(N/4)))))/(tanh(r))))));
        x_list_0_to_pidiv2.push_back(val);   
    }
    //---------------------------------------------------------------------
    vector<double> dx_list;
    double diff;
    for (int i = 0; i < (((x_list_0_to_pidiv2).size())-1); i  )
    {
        diff = x_list_0_to_pidiv2[i 1]-x_list_0_to_pidiv2[i];
        dx_list.push_back(diff);
    }
    //----------------------------------------------------------------------
    // reversing the dx_list
    reverse(dx_list.begin(), dx_list.end());
    //----------------------------------------------------------------------
    vector<double> x_list;
    double entry1,entry2;
    for (int i = 0; i < ((x_list_0_to_pidiv2).size()); i  )
    {
        entry1 = x_list_0_to_pidiv2[i];
        x_list.push_back(entry1);
    }
    for (int k = 0; k < (dx_list.size()); k  )// equi to line 28 py
    {
        entry2 = x_list[k (dx_list.size())]   dx_list[k];
        x_list.push_back(entry2);
    }
    //----------------------------------------------------------------------
    vector<double> dx_final_list;
    double diff2,val2;
    for (int i = 0; i < (x_list.size()-1); i  )
    {
        diff2 = x_list[i 1] - x_list[i];
        dx_final_list.push_back(diff2);
    }
    for (int i = 0; i <(dx_final_list.size()); i  )
    {
        val2 = x_list[i dx_final_list.size()]   dx_final_list[i];
        x_list.push_back(val2);
    }
    //----------------------------------------------------------------------
    vector<double> y_list;
    double val3;
    for (int i = 0; i < x_list.size(); i  )
    {
        val3 = x_list[i];
        y_list.push_back(val3);
    }

The error is as follows:

trialc  .cpp: In function 'double x_i(int)':
trialc  .cpp:20:5: error: 'x_list' was not declared in this scope
     x_list[node_number-1];
     ^~~~~~
trialc  .cpp: In function 'double y_j(int)':
trialc  .cpp:25:5: error: 'y_list' was not declared in this scope
     y_list[node_number-1];
     ^~~~~~

Note: List is written just for namesake, the datatype is vector everywhere Any help is highly appreciated!!

CodePudding user response:

x_list and y_list are local variables inside of main(), so they are out of scope of the code in x_i() and y_j(). You would have to pass in the variables as extra parameters to those functions, just like you do with display(), eg:

double x_i(const vector<double> &x_list, int node_number)
{
    return x_list[node_number-1];
}

double y_j(const vector<double> &y_list, int node_number)
{
    return y_list[node_number-1];
}

...

int main(){
    ...
    vector<double> x_list;
    vector<double> y_list;
    double d;
    ...
    d = x_i(x_list, some_node_number);
    ...
    d = y_j(y_list, some_node_number);
    ...
}
  • Related