ambiguity error in distance please look | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

ambiguity error in distance please look

#include<iostream> #include<cmath> using namespace std; class distance{ public: int x, y; friend void add(distance,distance); distance(int t,int k){ x=t; y=k; } void display(){ cout<<"The Point is : ("<<x<<","<<y<<")"<<endl; } }; void add(distance d1,distance d2){ int xd=(d2.x-d1.x); int yd=(d2.y-d1.y); int ans=sqrt(pow(xd,2)+pow(yd,2)); cout<<"The addition using distance formula is "<<ans<<endl; } int main(){ distance d(1,2); d.display(); distance d(2,3); d.display(); add(d,d1); return 0; }

23rd Feb 2023, 1:47 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
2 Answers
+ 2
I would first suggest you to choose a different name for the class, perhaps `Distance` makes a better name? Why? first because naming convention suggests class names begin with uppercase alphabet Second, because it triggers name conflict. Since your code assumes name resolution should refer `std` namespace ... using namespace std; Then `distance`- your class name, creates an ambiguity for the compiler, since it triggers name conflict with std::distance() function defined in header <iterator> https://en.cppreference.com/w/cpp/iterator/distance You can avoid this by removing the `using namespace std;` line, and use `std::` prefix for anything that was defined in `std` namespace e.g. std::cout and std::endl, or you can rename your class, choice is yours ... And I think the second instance in main() should be named <d1> - you have declared <d> twice, but looks like it was just a typo Next time please, attach a code bit link instead of raw text code, easier to check, and allows line number reference.
23rd Feb 2023, 3:35 PM
Ipang
0
Yup
23rd Feb 2023, 5:15 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar