使用带有“double”操作数的operator ^会发出编译错误

时间:2012-10-09 17:17:45

标签: c++

我已经提出了一些代码而且没有编译。

错误1错误C2296:'^':非法,左操作数的类型为'double'     2 IntelliSense:表达式必须具有整数或枚举类型

我能够提出的代码如下:

#include<iostream>
#include<cmath>
using namespace std;

void getnumbers();
void showmean();
void showdev();

double x1, x2, x3, mean, dev;

void getnumbers()
{
    cout<<"Please enter three numbers\n";
    cin>>x1,x2,x3;
}

void showmean()
{
    mean=(x1+x2+x3)/3;

    cout<<"The mean of"<<x1<<", "<<x2<<", "<<x3<<" is "<<mean<<endl;
}

void showdev()
{
    dev=sqrt((x1 - mean)^2) + (x2 - mean)^2 + (x3 - mean)^2/3;

    cout<<"The standard deviation of"<<x1<<", "<<x2<<", "<<x3<<" is "<<dev<<endl;
}

int main()
{
    getnumbers();
    showmean();
    showdev();

    system("pause");

    return 0;
}

2 个答案:

答案 0 :(得分:6)

  1. 你不能在C ++中采取类似的权力

    • 使用std::pow()x*x进行数学x 2
  2. 对于多个输入,它是cin >> x1 >> x2 ...etc

  3. 在您的SD等式中,我认为你的近距离在数学上是错误的。

    • 应为SD = sqrt( (x*x + y*y) / z )。你的近距离使其成为x + y * y / 3(或y 2/3 ,忘记优先权)

答案 1 :(得分:2)

除了用于平方值的错误运算符和缺少的括号之外,给定程序不满足赋值函数返回计算值的赋值部分。例如:

double calculate_mean()
{
    return (x1+x2+x3)/3;
}