即使在c++

发布于 2025-01-22 10:09:36 字数 3183 浏览 0 评论 0原文

我创建了两个向量,x_listy_list。我有两个函数在int main命名x_iy_j之前声明,这些功能用于在调用该值之前提取元素一个。例如,如果x_i(2)称为x_list [1],则与y_j的答案相同。我已经阅读了有关此问题的一些答案,说变量应该在范围内,但是我可以在代码中找到这个问题。

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

错误如下:

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];
     ^~~~~~

注意:列表仅用于同名,数据类型无处不在 任何帮助都将受到高度赞赏!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2025-01-29 10:09:36

x_listy_listmain(Main()的本地变量,因此它们不超出x_i()x_i()中的代码范围。 /code>和<代码> y_j()。就像您使用display(),例如:

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);
    ...
}

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);
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文