-2

I was trying to write a function with a return type of struct variable. If I use using namespace std; I get an error but if instead use std:: the program runs fine.

The erroneous code:

#include<iostream>
using namespace std;

struct distance
{
    int feet;
    int inches;
};
distance foo(distance, distance);
int main()
{
    distance d1, d2;
    cout << "Input feet of d1: ";     cin >> d1.feet;
    cout << "\nInput inches of d1: "; cin >> d1.inches;
    cout << "\nInput feet of d2: ";   cin >> d2.feet;
    cout << "\nInput inches of d2: "; cin >> d2.inches;
    distance large = foo(d1, d2);
    cout << "The larger distance is: " << large.feet << "\'-" << large.inches << "\"";
}
distance foo(distance d1, distance d2)
{
    float temp1 = d1.feet + d1.inches/12;
    float temp2 = d2.feet + d2.inches/12;
    if(temp1>temp2) return d1;
    else return d2;
}

Error: Reference to distance is ambiguous.

Working code without namespace std:

#include<iostream>

struct distance
{
    int feet;
    int inches;
};
distance foo(distance, distance);
int main()
{
    distance d1, d2;
    std::cout << "Input feet of d1: "; std::cin >> d1.feet;
    std::cout << "\nInput inches of d1: "; std::cin >> d1.inches;
    std::cout << "\nInput feet of d2: "; std::cin >> d2.feet;
    std::cout << "\nInput inches of d2: "; std::cin >> d2.inches;
    distance large = foo(d1, d2);
    std::cout << "The larger distance is: " << large.feet << "\'-" << large.inches << "\"";
}
distance foo(distance d1, distance d2)
{
    float temp1 = d1.feet + d1.inches/12;
    float temp2 = d2.feet + d2.inches/12;
    if(temp1>temp2) return d1;
    else return d2;
}

As far as I know namespace std has objects like cout, cin etc. But what does it have to do with structures? Why does using namespace std give error while directly using std:: runs the program smoothly?

user31782
  • 7,087
  • 14
  • 68
  • 143
  • `Error: Reference to distance is ambiguous.` What do you think that could possibly mean? – juanchopanza Mar 19 '16 at 10:14
  • 2
    Because there's [`std::distance`](http://en.cppreference.com/w/cpp/iterator/distance) – πάντα ῥεῖ Mar 19 '16 at 10:14
  • @πάνταῥεῖ Thanks. I didn't know that there is an distance structure in std namespace. – user31782 Mar 19 '16 at 10:19
  • It's too bad there is no way to search this kind of thing on the web. – juanchopanza Mar 19 '16 at 10:22
  • @juanchopanza I searched it many times but couldn't find nothing useful. – user31782 Mar 19 '16 at 10:25
  • 1
    @user31782 There also many other common words in std namespace: function, plus, less, set, pair, copy, unique... – Revolver_Ocelot Mar 19 '16 at 10:27
  • First hit I get: http://stackoverflow.com/questions/21079186/constructors-basic-reference-to-distance-is-ambiguous – juanchopanza Mar 19 '16 at 10:28
  • @juanchopanza I was searching with _reference to structure is ambiguous_, _reference to structure tag is ambiguous_ and _reference to structure variable is ambiguous_. I thought _distance_ is particular struct tag of my program so other question might have different tags. I accept that I didn't search in _Search engines_ well. I accept the downvotes. And thanks for mentioning the would-be reason of downvotes. – user31782 Mar 19 '16 at 10:38
  • I think your first hit http://stackoverflow.com/q/21079186/3429430 is a better duplicate for my question. Or perhaps both these questions are duplicate of the more general question http://stackoverflow.com/q/1452721/3429430 – user31782 Mar 19 '16 at 10:42

2 Answers2

1

As the message sugggests, the name distance is ambigious between std::distance and the structure you defined.

You can write

using std::cin;
using std::cout;
// more using for identifiers from namespace std to use

instead of using namespace std;

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

You shouldn't use using namespace std; exactly because this (and even worse) might happen.

In your case the compiler doesn't know which distance you are referring to: It could be either your own or std::distance

If you want to avoid writing std::cout every time you can just write using std:: cout. That will tell your compiler where to look when you use cout.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
  • Isn't writing `std::cout` shorter than `using std:: cout`? – user31782 Mar 19 '16 at 10:23
  • No, you got me wrong. instead of `using namespace std;` you write `using std::cout; using std::endl;`. You can then use `cout` and `endl` directly without having to add the prefix `std::` every time. – Simon Kraemer Mar 19 '16 at 10:26
  • Oh. Means we need not to invoke the whole namespce rather we can tell the compiler to add certain elements like `cout` or `cin` by writing using std::cout instead of `using namespace std;`. – user31782 Mar 19 '16 at 10:33
  • 1
    Exactly. Like in @MikeCAT's answer – Simon Kraemer Mar 19 '16 at 10:35