【问题标题】:" 'yc' can not be used as a function" error in C++C++ 中的“'yc' 不能用作函数”错误
【发布时间】:2017-09-22 06:55:04
【问题描述】:

我正在使用 C++ 计算 NACA 4 位翼型坐标。在这段代码中,我使用了犰狳库的 linspace 函数将 x 划分为线性间隔的点。当我使用 for 循环为每个 x 的值计算 yc 的值时,我得到错误 “yc”不能用作函数。感谢您的帮助。

#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(i) 应该是什么??
  • Yc 是弧线方程。
  • 您的代码和响应没有意义。如被问及,你想要yc(i) 是什么?
  • 无论yc 对您意味着什么,您都将它声明为一个简单的float 变量,而您实际上是在尝试将其作为函数调用。看起来您正在尝试使用在 for 循环中计算的值填充浮点数组。每个编译器都会抱怨,如果你不使用它的语言与之交谈。
  • 可能与stackoverflow.com/q/46223437/2932052 重复? ;)

标签: c++ compiler-errors


【解决方案1】:

yc 是一个浮点数。

编译器将symbol( ) 视为函数调用。这就是错误的含义。

也许创建一个 yc 数组

float yc[gp];

并使用

yc[i] = ....

如突出显示 - yc[gp] 可能无法正常工作,所以

float * yc = new float[gp];

最后是main()

delete []yc;

【讨论】:

  • VLA 不是标准 c++。
  • 也许使用std::vector&lt;float&gt; 会是更好的解决方案。如果提问者显然是 C++ 新手——为什么要引导他们使用“C-ish 解决方案”?
  • std::vector 还有助于避免可能(正式)由 OP 显示的 if 样式引起的未初始化数据,对我来说 that looks easier
【解决方案2】:

下面是一个示例,您可以如何将std::vector 用于此类任务。

vector&lt;float&gt; v(size); 的声明用float 类型的size 值填充向量,这些值都设置为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;
}

See the live demo on ideone.com ...

【讨论】:

    猜你喜欢
    • 2017-09-13
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 2017-11-08
    • 2018-07-23
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多