Error reference to 'distance' is ambiguous

1.5k Views Asked by At

I cannot figure out why I am getting the error "reference to 'distance' is ambiguous".
I have passed class object as an argument in friend function.

#include <iostream>
using namespace std;

class distance {
    int meters = 0;
public:
    distance() {}
    void displaydata() {
        cout << "Meters Value:" << meters;
    }
    //Prototype
    friend void addvalue(distance &d);
};

void addvalue(distance &d) {
    d.meters += 5;
}

int main() {
    distance d1; // meters = 0
    d1.displaydata(); // 0
    // The friend function call
    addvalue(d1); // pass by reference
    d1.displaydata();
}
1

There are 1 best solutions below

2
On

If you remove the using namespace std; and change cout to std::cout, then your code compiles w/o error.

Discovering the source of the ambiguity is left as an exercise to the reader