即使在 C++ 中的 int main 之前声明函数后,函数也未在范围内声明
Function not declared in scope even after declaring it before int main in 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];
^~~~~~
注意:List是为了同名而写的,数据类型到处都是vector
非常感谢任何帮助!!
x_list
和 y_list
是 main()
内部的局部变量,因此它们超出了 x_i()
和 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
和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];
^~~~~~
注意:List是为了同名而写的,数据类型到处都是vector 非常感谢任何帮助!!
x_list
和 y_list
是 main()
内部的局部变量,因此它们超出了 x_i()
和 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);
...
}