Problem with Pointer

Hello,
I'm writing a simple programm that calculates the distance between two points and the length of the line connecting several points.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cmath>

using namespace std; 


struct Point
{
   double x; 
   double y; 
};


struct Route 
{
   Point data; 
   Route* next;        
};

double distance(Point &a, Point &b);
double length(Route *s);


int main()
{
    
   Point p1={2.4,1.2};
   Point p2={-2,3.5};
   Point p3={1.0,-1.0};
   
   Route *r1= new Route;
   Route *r2= new Route;
   Route *r3= new Route;
   
   (*r1).data=p1;
   (*r1).next=r2;
   (*r2).data=p2;
   (*r2).next=r3;
   (*r3).data=p3;
   (*r3).next=NULL;
   
   Route *first=r1; 
   
   cout <<" The length of a hurricaine track is: " <<length(first)<<endl; 
   
   system("Pause"); 
   
   return 0; 
}


double distance(Point& a, Point& b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) +(a.y-b.y)*(a.y-b.y));      
}

double length(Route *s)
{
  double gesDistance=0; 
  
  while(s->next!=0)
  {
     Route* following=s->next;
                   
     gesDistance = gesDistance + distance(s->data,following->data);
     s=s->next; 
  }     
  
  return gesDistance; 
}


The line which makes problems is:
gesDistance = gesDistance + distance(s->data,following->data);


unfortunately I don't get the error of the compiler since the error is shown in a completly different file, which I didn't create. It says something about Point Iterators?!

Thanks for help ;-)
That sounds like a name conflict. There is a std::distance that may be coming into scope by using namesapce std;. Try renaming your distance function, wrapping it in a namespace and/or using the scope resolution operator.
Last edited on
Your distance() function is not being called because of using namespace std;

There's a standard library function distance() that returns the distance between two iterators.
Thanks a lot for your help :-)

Now I get another error:
[Linker error] undefined reference to `Info::length(Route*)'
[Linker error] undefined reference to `Info::distance(Point&, Point&)'


I updated my code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
namespace Info
{
   double distance(Point &a, Point &b);
   double length(Route *s);
}

// and

cout <<" The length of a hurricaine track is: " << Info::length(first)<<endl; 



// and

gesDistance = gesDistance + Info::distance(s->data,following->data);



I don't get the error...
But changing the name of the function distance to myDistance(...) did work. So thanks a lot !!!

Nonetheless I would be really thankful about a small explanation what I did wrong with my namespace.
You probably forgot to qualify the names in your function definitions.
Topic archived. No new replies allowed.