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; }
3 Respuestas
+ 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.
0
Yup