ambiguous error (3d distance calculator)

closed account (D2yM4iN6)
code removed
Last edited on
Try changing
#include<math.h> to #include<cmath> .
It may be due to math.h containg only abs(int) overload of this function.
You need to replace cdist = max(x,y,z) with a comparison.
1
2
3
4
5
6
7
8
9
10
11
12
13
if(x >= y) //Find the greater of x and y
    {
        max = x;//If x is the max (or they are equal)
    }
    else //Otherwise
    {
        max = y; //If y is the max 
    }
    if(z > max) //Compare z to the largest of x and y. 
    {
        max = z;
    }
    cdist = max;
There's an interesting version of max() declared in the C++11 <algorithm> header. Notice line 24 in particular.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;
int main()
{
    srand (time(NULL));

    for (int i=0; i<10; i++)
    {
        int x = rand()%100;
        int y = rand()%100;
        int z = rand()%100;
        
        cout << setw(4) << x
             << setw(4) << y
             << setw(4) << z
             << "  max = "
             << setw(4) << max(max(x, y), z)   // call max twice
             << "  max2 = "
             << setw(4) << max({x, y, z})      // call max with initialiser list
             << endl;
    }

    return 0;
}

Output:

  93   5   7  max =   93  max2 =   93
  85  46  17  max =   85  max2 =   85
  77  48  42  max =   77  max2 =   77
  38  14  73  max =   73  max2 =   73
  84  57  69  max =   84  max2 =   84
  12  89  32  max =   89  max2 =   89
  82  72  19  max =   82  max2 =   82
  32  12  70  max =   70  max2 =   70
  39  19  66  max =   66  max2 =   66
  98  26   0  max =   98  max2 =   98

http://www.cplusplus.com/reference/algorithm/max/
http://www.cplusplus.com/reference/initializer_list/initializer_list/
closed account (D2yM4iN6)
thank you all, especially with the example!! It helped me alot and was able to understand much more clear with the concept of max()
Topic archived. No new replies allowed.