试图对C++接收错误消息
试图写在我使用罪程序中使用罪,但一直收到错误讯息试图对C++接收错误消息
“罪
错误:重载函数的一个以上的实例”罪“匹配参数列表”
任何想法我做错了什么?
#include <stdio.h>
``#include <math.h>
#define DEGREE 45
#define THREE 3
int
main(void)
{
double diagonal;
double length;
double volume;
double stands;
double volumeostands;
//Get # of stands
printf("How many stands will you be making? \n");
scanf("%d", &stands);
//Get diagonal//
printf("What is the diagonal of the cube? \n");
scanf("%d", &diagonal);
//Find length
length = diagonal * sin(double(DEGREE);
//Find volume
volume = 3(length);
//Multiply volume by number of stands
volumeostands = volume * stands;
//Display volume (for # of stands)
printf("Volume is %lf inches for %lf stands", &volumeostands, &stands);
return (0);
}
这时候,编译器无法弄清楚这重载函数去调用你的论点,你得到错误信息,它的通常做类型提升或转换。例如,你可以有:
void fn(double d);
void fn(float f);
,如果你调用fn(x)
其中x
既不是float
也不double
但同样可以成为这些类型之一,编译器将不知道该选哪。以下程序显示了这样的情景:
#include <iostream>
int x(double d) { return 1; }
int x(float f) { return 2; }
int main(){
std::cout << x(42) << '\n';
return 0;
}
编译与g++
结果:
qq.cpp: In function ‘int main()’:
qq.cpp:5:20: error: call of overloaded ‘x(int)’ is ambiguous
std::cout << x(42) << '\n';
^
qq.cpp:5:20: note: candidates are:
qq.cpp:2:5: note: int x(double)
int x(double d) { return 1; }
^
qq.cpp:3:5: note: int x(float)
int x(float f) { return 2; }
因为你传递的int
值同样可以成为float
或double
,编译器会抱怨。一个快速的解决办法是把该类型的特定一个喜欢的东西:
std::cout << x((double)42) << '\n';
为了您具体情况下,它可能正是我所展示的,在你打电话sin()
与一个完整的类型。在C++ 11之前,唯一的重载是float
,double
和long double
。 C++ 11引入了整数类型的重载,它们将它们提升为double
,但如果你不是,则可以使用C++ 11的,只需使用上面显示的转换技巧即可。
如果是(使用整型)的情况下,你应该意识到的第一件事是,sin()
接受其参数为弧度而不是度数,所以你几乎肯定会希望使用浮点(圆周有360°,但只有2π弧度)。
#include <stdio.h>
#include<iostream>
#include <math.h>
using namespace std;
#define DEGREE 45
#define THREE 3
int main(void)
{
double diagonal;
double length;
double volume;
double stands;
double volumeostands;
//Get # of stands
cout << "How many stands will you be making? \n" ;
cin >> stands;
//Get diagonal//
cout << "What is the diagonal of the cube? \n" ;
cin >> diagonal;
//Find length
length = diagonal * sin(DEGREE);
//Find volume
volume = 3*length;
//Multiply volume by number of stands
volumeostands = volume * stands;
//Display volume (for # of stands)
cout << "Volume is " << volumeostands << " inches for stands " << stands << endl;
system("pause");
return 0;
}
通常,答案会描述您所做的事情,并且格式正确。有趣的是,这段代码更有可能产生最初问到的问题,即如果没有缺少括号,问题中发布的代码将不会发布。 – 2014-12-04 06:33:20
你能后的代码,以便我们可以帮助? – Poriferous 2014-12-04 02:44:21
啊哈!所以问题是,“我的代码是什么?”。我猜测代码中包含一个名为“sin”的函数调用。 – 2014-12-04 02:49:28
您发布的代码不会编译,并且有许多其他错误,比如使用'%d'而不是'%f'读取/打印双精度数据,并将值的地址传递给'printf'时它应该是值。请发布一些正确的代码。 – 2014-12-04 04:36:10