“ 'yc' can not be used as a function” erro

2019-09-26 03:16发布

问题:

I am calculating NACA 4 digit airfoil coordinates using C++. In this code I used armadillo library's linspace function to divide x into linearly spaced points. When I use a for loop to calculate yc's value for each x's value I get error "yc" can not be used as function. Thanks for helping.

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

回答1:

yc is a single float.

The compiler treats symbol( ) as a function call. That is what the error means.

Perhaps create an array of yc

float yc[gp];

and use

yc[i] = ....

As highlighted - yc[gp] may not work, so

float * yc = new float[gp];

And at the end of main()

delete []yc;


回答2:

Here is an example how you can use a std::vector for tasks of this kind.

The declaration of vector<float> v(size); fills the vector with size values of type float that are all set to 0.0 which is the standard value for 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;
}

See the live demo on ideone.com ...