我正在使用C ++计算NACA 4位翼型坐标。在这段代码中,我使用了armadillo库的linspace函数将x分成线性间隔的点。当我使用for循环计算每个x
值的#include<iostream>
#include<armadillo>
#include<vector>
using namespace std;
using namespace arma;
int main()
{
float a[3];
float c;
int gp = 100;
cout << "Please Enter NACA 4 digits" << endl;
cout << "Please Enter 1st digit" << endl;
cin >> a[0] ;
cout << "Please Enter 2nd digit" << endl;
cin >> a[1] ;
cout << "Please Enter last 2 digits" << endl;
cin >> a[2] ;
cout << "Please Enter Chord Length" << endl;
cin >> c;
float m=(a[0]*c)/100;
float p=(a[1]*c)/10;
float t=(a[2]*c)/100;
cout << m << endl;
cout << p << endl;
cout << t << endl;
vec x = linspace<vec>(0, c, gp);
float yc;
for(int i=0;i<gp;++i)
{
if (x(i) = 0 && x(i) <= p){
yc(i) = (m/(p*p))*((2*p*(x(i)))-(x(i)*x(i)));
}
if (x(i) > p && x(i) <= c) {
yc(i) =(m/((1-p)*(1-p)))*((1-(2*p))+(2*p*x(i))-(x(i)*x(i)));
}
}
cout<< yc <<endl;
return 0;
}
值时,我得到错误“yc”不能用作函数。谢谢你的帮助。
.intro > div {
float:left;
clear: both;
display:block;
}
答案 0 :(得分:1)
yc
是一个浮动。
编译器将symbol( )
视为函数调用。这就是错误的含义。
也许创建一个yc数组
float yc[gp];
并使用
yc[i] = ....
如突出显示 - yc[gp]
可能不起作用,所以
float * yc = new float[gp];
在main()
delete []yc;
答案 1 :(得分:0)
以下是如何使用std::vector
进行此类任务的示例。
vector<float> v(size);
声明使用size
类型为float
的值填充向量,这些值均设置为0.0
,这是float
的标准值:
#include <iostream>
#include <vector>
using namespace std;
// demonstrates how a vector is used as a better variadic array:
void vector_usage(int size){
// initializing a vector with size 0 values
std::vector<float> v(size);
// fill in some (not very meaningful) values
for(int i=0; i<size; ++i) {
if ((4 <= i) && (i < 8))
v[i] = 15.0/i;
}
// inspect the result on console:
for (auto e: v) {
cout << e << " ";
}
cout << endl;
// hopefully having learned something ;)
}
int main() {
vector_usage(12);
return 0;
}