我无法弄清楚我的代码有什么问题!我的代码看起来与教授的副本完全相同,但由于第25行(最后E0109
语句)中的错误C2064
和cout
而无法运行。
我是否需要为product
和sqrt
变量编写方程式,或者cmath
会自动处理这个变量吗?
我的问题肯定是使用pow()
/ sqrt()
函数。
输出应显示:
Enter the base: 3 Enter the exponent: 2 3 to the 2 power equals 9. The square root of 3 equals 1.73. Press any key to continue
我的完整代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int base;
int exponent;
int product;
float sqrt;
cout << "Enter the base: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
cout << base << " to the " << exponent << " power equals " << pow(base, exponent);
cout << "The square root of " << base << " equals " << sqrt(base) << ".\n\n";
return 0;
}
答案 0 :(得分:6)
您命名了一个局部变量sqrt
,它隐藏了sqrt()
中的cmath
函数。然后,当您尝试调用sqrt(base)
时,编译器必须解析sqrt
名称,因此它首先查找本地范围并找到sqrt
变量,这不是函数,因此无法使用()
运算符调用。
将变量的名称更改为squareRoot
,或其他任何非sqrt
的名称。
另外,你还没有真正使用那个变量,所以严格来说这不是必需的,可以删除。