即使在c++
我创建了两个向量,x_list
和y_list
。我有两个函数在int main命名x_i
和y_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_list
and 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
x_list
和y_list
是main(Main()
的本地变量,因此它们不超出x_i()
x_i()中的代码范围。 /code>和<代码> y_j()
。就像您使用display()
,例如:x_list
andy_list
are local variables inside ofmain()
, so they are out of scope of the code inx_i()
andy_j()
. You would have to pass in the variables as extra parameters to those functions, just like you do withdisplay()
, eg: