【问题标题】:C++ invalid operands and typesC++ 无效的操作数和类型
【发布时间】:2013-12-07 18:16:55
【问题描述】:

在我的 A 和 B 方程中得到这些错误,然后其他错误来自 calcit 的末尾,当我试图将它传递给 slopeit 时

[错误] 'int [3]' 和 'int [3]' 类型的无效操作数到二进制 'operator*' [错误] 'double' 和 'int [3]' 类型的无效操作数到二进制 'operator*' [错误] 从 'int' 到 'double*' 的无效转换 [-fpermissive] [错误] 无法将参数 '2' 的 'int*' 转换为 'double*' 到 'void slopeit (double*,double*, int, double&, double&, double&)'
     double slops[3], yints[3], boards[3];
     double yint15,yint20,yint25,slop15,slop20,slop25,rsq15,rsq20,rsq25;
     double board;

    void calcit (double tim15[], double tim20[], double tim25[], double tem15[],
        double tem20[], double tem25[], int indx, int board,int temperature)
     {
double B;
double A;
double time;
double slopsofslops;
double yofslopes;
double rsq;
double yint15,yint20,yint25,slop15,slop20,slop25,rsq15,rsq20,rsq25;
slopeit(tim15, tem15, indx, slop15, yint15, rsq15);
slopeit(tim20, tem20, indx, slop20, yint20, rsq20);
slopeit(tim25, tem25, indx, slop25, yint25, rsq25);


yints[0]=yint15;               
yints[1]=yint20;
yints[2]=yint25;

boards[0]=15;
boards[1]=20;
boards[2]=25;

slops[0]=slop15;
slops[1]=slop20;
slops[2]=slop25;


indx = 3;


time = pow(e,(temperature -B)/A);
A = (slops * boards) + yofslopes;
B = (yofslopes * boards) + yints; 

//Pass the values needed into writeit and finished 

slopeit(board, slops, indx, slopsofslops, yofslopes, rsq);
       }
  void slopeit(double x[], double y[], int n, double& m, double& b, double& r)

【问题讨论】:

    标签: c++ arrays binary operands


    【解决方案1】:

    C++ 没有任何内置运算符来操作数组,您必须创建自己的重载。

    至于最后的错误,int 的数组(或指针)与double 的数组(或指针)不同。您必须创建一个新的临时double 数组,从int 数组中填充它,并将double 数组传递给函数。

    【讨论】:

      【解决方案2】:

      在调用 slopeit() 时,您使用 board 而不是 board 来调用第一个参数。 board 是 double,boards 是 double[]。

      【讨论】:

        【解决方案3】:

        你需要根据你的定义传递指向函数的指针

          slopeit(board, slops, indx, *slopsofslops, *yofslopes, *rsq);
               }
          void slopeit(double x[], double y[], int n, double& m, double& b, double& r)
        

        【讨论】:

          【解决方案4】:

          [错误] 'int [3]' 和 'int [3]' 类型的无效操作数转为二进制 '操作员*'

          此错误是由以下行引起的:

          A = (slops * boards) + yofslopes;
          

          slops 和 board 都是 double[3] 类型。 C++ 不能乘以数组。您需要使用可以支持它的不同类,例如 Qt 库中的 QVector3D 类,或者您需要自己在 for 循环中计算乘积(叉积或点积)。

          [错误] 'double' 和 'int [3]' 类型的无效操作数到二进制 '操作员*'

          此错误是由以下行引起的:

          B = (yofslopes * boards) + yints; 
          

          yofslopes 是 double 类型,而 board 是 double[3]。同样,C++ 不支持执行这些类型的操作。它们是不兼容的类型。您可能需要执行一个 for 循环来将每个元素乘以 yofslopes(您在此处追求的是什么?)。您也不能将一个数组添加到另一个数组。

          不清楚您要在这里做什么,因为这是该行的单元分析:

          double = (double * 3dVector) + 3dVector
          

          这没有意义......

          [错误] 从 'int' 到 'double*' 的无效转换 [-fpermissive]

          此错误来自以下行:

          slopeit(board, slops, indx, slopsofslops, yofslopes, rsq);
          

          您有一个名为 board 的全局变量,它是 double 类型(不是 double*)。然后你定义了一个同名的局部变量(在 calcit 的参数中),但类型为 int(不是 double*)。你不应该传入一个整数并将其解释为一个指针而不显式转换它。

          [错误] 无法将参数 '2' 的 'int*' 转换为 'double*' 到 'void 斜率

          不确定此错误表示什么。

          希望这会有所帮助!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-21
            • 1970-01-01
            • 2013-01-25
            • 1970-01-01
            • 2015-12-02
            • 2014-09-30
            相关资源
            最近更新 更多