C++ 对“ ”的引用不明确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29387600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reference to ' ' is ambiguous
提问by damian
I am sorry but i don't know why this algorithm is not working. The error at compiling is : "Reference to 'function' is ambiguous " and is on y = function()line, where I am calling the function
我很抱歉,但我不知道为什么这个算法不起作用。编译时的错误是:“对‘函数’的引用不明确”并且在y = function()行上,我在那里调用函数
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
float function(int g, int m, int s, float z)
{
using namespace std;
z = (g + m/60.0 + s/3600.0)*PI/180.0;
return z;
}
int main()
{
using namespace std;
float y;
int g,m,s;
cout << "g = ";
cin >> g;
cout <<"m = ";
cin >> m;
cout<<"s= ";
cin >>s;
y = function();
cout << "y= " << y << endl;
//cout<< (g + m/60.0 + s/3600.0)*PI/180.0 << endl;
return 0;
}
Vers2 - updated:
Vers2 - 更新:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
float function(int g, int m, int s)
{
//using namespace std;
float z = (g + m/60.0 + s/3600.0)*PI/180.0;
//std::cout << z <<std::endl;
return z;
}
int main()
{
// using namespace std;
float y;
int g,m,s;
std::cout << "g = ";
std::cin >> g;
std::cout <<"m = ";
std::cin >> m;
std::cout<<"s= ";
std::cin >>s;
function();
// std::cout << "y= " << y << std::endl;
//cout<< (g + m/60.0 + s/3600.0)*PI/180.0 << endl;
return 0;
}
回答by poe123
There is a member function
in std and you inserted it into your namespace. Avoid using using namespace std;
; you can import what you need this way:
function
std 中有一个成员,您将其插入到您的命名空间中。避免使用using namespace std;
; 您可以通过这种方式导入您需要的内容:
using std::cout;
using std::cin;
回答by MikeMB
I can't reproduce your error message (for any of your versions with 3 different compilers), but the basic problem with your code is that you apparently assume the g,m,s
-variables in your main functions are automatically used as parameters when you call function()
just because they happen to have the same name.
我无法重现您的错误消息(对于您使用 3 种不同编译器的任何版本),但是您的代码的基本问题是您显然假设g,m,s
主函数中的-variables 在您调用时自动用作参数,function()
因为它们碰巧有相同的名字。
This is NOT the case!
不是这种情况!
The variables inside your main and in the parameter list of function() are completely independent entities. The proper way to call the function and passing the right values is this:
main 中的变量和 function() 的参数列表中的变量是完全独立的实体。调用函数并传递正确值的正确方法是:
y=function(g,m,s);
This basically copies the values stored inside the main g,m,s
variables into the g,m,s
parameters, which are accessed inside the function and after the function has completed, it then copies the value stored inside the variable you "return" from the function (here z
) into the variable y
.
这基本上将存储在主g,m,s
变量中的值复制到g,m,s
参数中,在函数内部访问这些参数,在函数完成后,它将存储在您从函数(此处z
)“返回”的变量中的值复制到变量中y
。
This should work whether you are using using namespace std;
or not, as your function has a completely different signature, But I'd still highly recommend to choose another name for your function.
无论您是否使用using namespace std;
,这都应该有效,因为您的函数具有完全不同的签名,但我仍然强烈建议为您的函数选择另一个名称。
I hope this doesn't sound like an insult, but I highly recommend that you read a introductory book about c++ programming, as it seems you are missing out on basic concepts of the language.
我希望这听起来不像是一种侮辱,但我强烈建议您阅读一本关于 c++ 编程的介绍性书籍,因为您似乎错过了该语言的基本概念。