Compilation errors

I am a Java programmer migrating to C++ and I am having occasional difficulties understanding the differences between references and pointers. I am getting a compilation error in this context. Here is my code to illustrate the problem and I will be grateful for any assistance.

First of all I am unhappy over the usage of the keyword "this" in my code and I am wondering whether there can be an alternate way to do the same functionality.

Secondly I am not sure where I am going wrong with the pointer assignment.


Aree.cpp: In member function ‘void ATree::foo(Point, double)’:
ATree.cpp:45:37: error: no match for call to ‘(Distance) (Point&, Point&)’
double dist = distance(query,point);
^

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

#include "ATree.h"

using namespace std;
using std::vector;


ATree::ATree(vector<Point> points,Distance pfunc)
{
  distance = pfunc;

  vp = points.front();
  points.erase(points.begin());

  vector<Point>::iterator it;
  vector<double> distances;
  for (it = points.begin();it != points.end();++it)
    {
      double d;
      Point point = *it;
      d = distance.calculateDistance(vp,point);
      distances.push_back(d);
    }
}

void ATree::foo(Point query, double maxDistance)
{

  vector<pair<ATree*,double>> nodes_to_visit;
  ATree* node;
  double d0;
  nodes_to_visit.push_back(make_pair(this,0));
  
  auto it  = nodes_to_visit.end();
  node = it->first;
  d0 = it->second;
  Point point = node->vp;
  
  double dist = distance(query,point);
	     
}


and

1
2
3
4
5
6
7
8
9
class Distance
{
 private:
  double distance;
  
 public:
  double calculateDistance(Point point1,Point point2);

};
Last edited on
"this" looks unavoidable here (without a re-write).


The error indicates you tried to make a class Distance object with something incorrect. But that call isnt shown.
1
2
d = distance.calculateDistance(vp,point);
double dist = distance(query,point);
¿see the difference?

points.erase(points.begin());
that's O(n)
however, .pop_back() is O(1)
ok, now I am confused by 'Distance' and 'distance'
... is there a caSe proBlem behind this?
Yes, C++ is case sensitive.
There are only two more files and these look like this. I have not included the full definitions of the code because I am only trying to get the compilation issue resolved. These are just mock ups.



1
2
3
4
5
6
7
8
9
10
#include "Distance.h"


double Distance::calculateDistance(Point point1,Point point2)
{
  double lat = point1.latitude;
  double lon = point2.longitude;
  distance = 0;
  return distance;
}


and then Point.h
1
2
3
4
5
struct Point
{
  double longitude,latitude;
};


This is how I compile my code -

g++ -c -g *.cpp


I changed distance to dist in Distant.h and Distant.cpp if it were indeed a case sensitivity issue. But I get the same compilation error.
Last edited on
ok, I'll try again
1
2
d = distance.calculateDistance(vp,point); //line 21, ATree constructor
double dist = distance(query,point); //line 39, foo 
you've got a `distance' object of `Distance' class
the first time you send it a message `.calculateDistance()'
the second time you simply open parenthesis *
¿see the difference?

by the way, there are free functions in c++, you don't need a class for everything.



* I suppose you don't know about operator overloading, you'll be sending the message .operator()
Does the ATree class represent a typical "tree" data structure? It sort of looks more like a linked list than a tree.

This looks a bit over-engineered, especially with what ne555 said, since you don't actually need a class just to calculate distances.

Perhaps if you told us what the end goal was here, we could present a more C++-y way to do things.
Last edited on
I thought java had added the ability to have subroutines? Does it STILL need an object for that?!
Registered users can post here. Sign in or register to post.