C++ 无效类型';浮动*[浮动]';数组下标
我想显示x和f(x)的范围,并将f(x)保留在数组中,但我总是遇到以下错误:C++ 无效类型';浮动*[浮动]';数组下标,c++,c++11,C++,C++11,我想显示x和f(x)的范围,并将f(x)保留在数组中,但我总是遇到以下错误: invalid type 'float*[float]' for array subscript 有人能帮我吗?我还是被卡住了 代码如下: #include <iostream> #include <cmath> #include <math.h> using std::cin; using std::cout; using namespace std; void display
invalid type 'float*[float]' for array subscript
有人能帮我吗?我还是被卡住了
代码如下:
#include <iostream>
#include <cmath>
#include <math.h>
using std::cin;
using std::cout;
using namespace std;
void displayValue(float funx[], float j, float x);
int main()
{
float num9[]={};
float a, r;
displayValue(num9, a, r);
return 0;
}
void displayValue(float funx[], float j, float x)
{
float i;
cout << "Please enter range of x: " << endl;
for (i=0; i<1; i++)
{
cin >> x >> j;
}
for (float i=1; i<=160.5; i++)
{
x+=0.5;
funx[i]=1/sin(x)+1/tan(x);
//1.2 Display f(x) and x within the range
}cout << x << " = " << funx[i] << "\n";
}
#include <iostream>
#include <cmath>
#include <math.h>
// Don't add "using namespace std", that separation exists for a reason.
// Separate the math function to make it clear what's being done
float f(const float x) {
return 1/sin(x)+1/tan(x);
}
// Define your functions before they're used to avoid having to declare
// then later define them.
void displayValue(const float min, const float max, const float step = 0.5)
{
for (float x = min; x <= max; x += step)
{
// Note how the f(x) function here is a lot easier to follow
std::cout << "f(" << x << ") = " << f(x) << std::endl;
}
}
int main()
{
std::cout << "Please enter range of x: " << std::endl;
// Capture the range values once and once only
float min, max;
std::cin >> min >> max;
// Display over the range of values
displayValue(min, max);
return 0;
}
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用名称空间std;
无效显示值(浮点funx[],浮点j,浮点x);
int main()
{
float num9[]={};
浮动a,r;
显示值(num9,a,r);
返回0;
}
无效显示值(浮点funx[],浮点j,浮点x)
{
浮点数i;
cout>j;
}
对于(float i=1;i您试图解决的问题实际上不是您需要解决的问题。此代码中有很多错误,可以简单地删除,因为您使用了错误的工具
这里不需要数组。如果你需要分配一个数组,而不是在空的东西中传递,或者你将使用它。在C++中,对于这样的数组,使用<代码> STD::vector < /C> >
也就是说,下面是代码的简化版本:
#include <iostream>
#include <cmath>
#include <math.h>
using std::cin;
using std::cout;
using namespace std;
void displayValue(float funx[], float j, float x);
int main()
{
float num9[]={};
float a, r;
displayValue(num9, a, r);
return 0;
}
void displayValue(float funx[], float j, float x)
{
float i;
cout << "Please enter range of x: " << endl;
for (i=0; i<1; i++)
{
cin >> x >> j;
}
for (float i=1; i<=160.5; i++)
{
x+=0.5;
funx[i]=1/sin(x)+1/tan(x);
//1.2 Display f(x) and x within the range
}cout << x << " = " << funx[i] << "\n";
}
#include <iostream>
#include <cmath>
#include <math.h>
// Don't add "using namespace std", that separation exists for a reason.
// Separate the math function to make it clear what's being done
float f(const float x) {
return 1/sin(x)+1/tan(x);
}
// Define your functions before they're used to avoid having to declare
// then later define them.
void displayValue(const float min, const float max, const float step = 0.5)
{
for (float x = min; x <= max; x += step)
{
// Note how the f(x) function here is a lot easier to follow
std::cout << "f(" << x << ") = " << f(x) << std::endl;
}
}
int main()
{
std::cout << "Please enter range of x: " << std::endl;
// Capture the range values once and once only
float min, max;
std::cin >> min >> max;
// Display over the range of values
displayValue(min, max);
return 0;
}
#包括
#包括
#包括
//不要添加“using namespace std”,因为存在分离是有原因的。
//将数学函数分开,使其清楚地显示正在执行的操作
浮点f(常数浮点x){
返回1/sin(x)+1/tan(x);
}
//在使用函数之前定义它们,以避免必须声明
//然后稍后定义它们。
无效显示值(常数浮点最小值、常数浮点最大值、常数浮点步长=0.5)
{
对于(float x=min;x)你知道使用std::cin
和使用std::cout
做什么吗?i
是一个float
。你不能使用float作为索引。你有什么理由在这里使用float
而不是int
?用标题来描述你的问题,这样人们可以一眼就知道他们是否能帮上忙。“有人能帮我吗"这不是问题。数组需要整数索引,所以你需要使用一个整数值来引用它们。最后:传递给j
和x
的值是不相关的,它们无论如何都会被覆盖。所以它们应该是局部变量。为了支持你的答案,我删除了我的答案,但我想你应该添加一个关于浮点的词e> 循环计数器。如果存在舍入错误,循环可能会在之前/之后停止一次迭代expected@idclev463035818值得注意的是,浮点数学是相当多的蠕虫,所以每次只能做一件事。我想说,虽然不推荐使用,但使用命名空间std使用也可以。不过,只能使用ei使用命名空间libraryName
和使用libraryName::functionName
,而不是两者都使用。