Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pair in C++

Tags:

c++

stl

Can someone please tell why I am unable to compile the following program:

#include<iostream>
#include<string>
#include<cmath>
#include<iostream>
#include<cfloat>
#define MOD 10000009

using namespace std;

double distance(pair<int,int> p1,pair<int,int> p2)
{
    double dist;
    dist = sqrt( (p1.first-p2.first)*(p1.first-p2.first) + (p1.second-p2.second)*(p1.second-p2.second) );
    return(dist);
}

int main()
{
        int N,i,j;
        cin >> N;
        pair<int,int> pi[N];
        for(i=0;i<N;i++)
        {
            cin >> pi[i].first >> pi[i].second;
        }

        for(i=0;i<N;i++)
        {
            cout << pi[i].first << " "<< pi[i].second << endl;
        }

        distance(pi[0],pi[1]); // This line is giving error

    return 0;
}

enter image description here

like image 721
user1543957 Avatar asked Mar 31 '26 15:03

user1543957


1 Answers

distance is defined by the standard library, which you already using'ed into the global namespace, so it's trying to use that one rather than the one you wrote (and expected).

Call your function something else to avoid the name clash.

Also just as style notes, in C++ code #define can usually be replaced with const or inline functions to provide much greater type safety, and I like to write using std::cout, etc for each item I need rather than using namespace std.

like image 107
Mark B Avatar answered Apr 02 '26 15:04

Mark B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!