18

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

#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:

#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;
}
damian
  • 183
  • 1
  • 1
  • 8

3 Answers3

19

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:

using std::cout;
using std::cin;
poe123
  • 1,188
  • 8
  • 11
  • 1
    @MSalters: I tried many ways to solve this, but none of them appears to work. I have deleted the "using namespace std" and put "std::" instead.Also, i have used z as local variable in my function and deleted from arguments. – damian Apr 01 '15 at 10:26
  • @damian Please update your code so we can analyze it further. – poe123 Apr 01 '15 at 10:56
  • @damian: Is it still the same error? I'd expect something about missing parameters – MikeMB Apr 01 '15 at 11:59
  • @ MikeMB: yes, it is the same error. Well, I had in my first version, z as parameter, but i have removed it and wrote it as local variable in my function. Please advise ! – damian Apr 01 '15 at 12:02
4

I am getting a similar type of error while I used "prev" as a global variable of Node* type. Just renaming it with "prevv" solved issue in my case.

It is mostly due to the name of a "variable or function" is present in some library you used.

1

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.

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:

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.

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.

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.

MikeMB
  • 20,029
  • 9
  • 57
  • 102