2016-06-01 10 views
0

Ich habe eine Std :: Liste, die ich auf der Grundlage einiger Berechnungen zu sortieren versuchen. Point2D ist eine Struktur mit nur int no, double x und double y;Benutzerdefinierte list.sort Vergleiche in C++

Hier ist die Methode, die meine List.Sort Code enthält:

std::vector<Point2D> GrahamScan::getSortedPointSet(std::vector<Point2D> points) { 
Point2D lowest = getLowestPoint(points); 

std::list<Point2D> list; 

for (int i = 0; i < (int)points.size(); i++) { 
    list.push_back(points[i]); 
} 

list.sort(compare_points); 

std::vector<Point2D> temp; 
for (int i = 0; i < (int)list.size(); i++) { 
    temp.push_back(list.front()); 
    list.pop_front(); 
} 
return temp; 
} 

Und hier ist die compare_points Methode, die ich schrieb:

bool GrahamScan::compare_points(const Point2D& a, const Point2D& b) { 
if (a.x == b.x && a.y == b.y) { 
    return false; 
} 

double thetaA = atan2((long)a.y - lowest.y, (long)a.x - lowest.x); 
double thetaB = atan2((long)b.y - lowest.y, (long)b.x - lowest.x); 

if (thetaA < thetaB) { 
    return false; 
} 
else if (thetaA > thetaB) { 
    return true; 
} 
else { 
    double distanceA = sqrt((((long)lowest.x - a.x) * ((long)lowest.x - a.x)) + 
     (((long)lowest.y - a.y) * ((long)lowest.y - a.y))); 
    double distanceB = sqrt((((long)lowest.x - b.x) * ((long)lowest.x - b.x)) + 
     (((long)lowest.y - b.y) * ((long)lowest.y - b.y))); 

    if (distanceA < distanceB) { 
     return false; 
    } 
    else { 
     return true; 
    } 
} 
} 

Der Fehler Visual Studio ist bei mir spuckt ist „Graham Scan: : compare_points ": Nicht-Standardsyntax; Verwendung ‚&‘ ein Zeiger auf ein Element erstellen“

Ich habe nicht viel Erfahrung in C haben ++, aber ich versuche, einige Java-Code zu konvertieren, die eine TreeSet zu C++ verwendet und dies ist mein Versuch.

Jede Hilfe würde geschätzt

+0

http://stackoverflow.com/a/4288479/212870 –

+3

die Vergleichsfunktion Stellen 'static' und versuche es erneut. Aber lassen Sie mich vor der * strict-weak-order * -Anforderung für die Vergleichsfunktion warnen. Ihre Implementierung ist komplex und es ist schwer zu sagen, ob sie dieser Anforderung entspricht. – PaulMcKenzie

Antwort

2

Wenn Sie compare_points in GrahamScan Namensraum halten wollen, müssen Sie es statisch machen.

static bool GrahamScan::compare_points 

der Grund der Compiler beschwert sich, dass compare_points ist eine Mitgliedsfunktion. Es benötigt ein Objekt GrahamScan, das angewendet werden soll. Hinter den Vorhängen ist die echte Funktionssignatur von compare_points etwas wie bool compare_points(GrahamScan *this, const Point2D& a, const Point2D& b). Also machen Sie es entweder statisch oder definieren Sie es nicht als Elementfunktion.

Sobald Sie compare_points statisch machen, ist Ihre niedrigste Variable nicht mehr zugänglich. Einfacher Weg, um die Arbeit am niedrigsten macht auch statisch:

class GrahamScan 
{ 
    // declaration is inside class 
    static Point2D lowest; 
} 

// definition is outside class 
Point2D GrahamScan::lowest; 

und es wie folgt verwendet werden:

std::vector<Point2D> GrahamScan::getSortedPointSet(std::vector<Point2D> points) 
{ 
    GrahamScan::lowest = getLowestPoint(points); 
    //... 
} 
+0

Jetzt bekomme ich ein "nicht aufgelöstes externes Symbol" privat: statische Struktur Point2D GrahamScan :: niedrigster "" Fehler. Ich habe versucht, meine Point2D Variable als statisch vergebens zu deklarieren. – awbasham

+0

@awbasham Stellen Sie sicher, dass Sie eine statische Variable ordnungsgemäß deklarieren. Wenn du '' 'lowest'''static machst, dann benutze' GrahamClass' '' '' '' '' '' '' '' 'statischen Punkt Point2D lowest''', setze' '' Point2D GrahamScan :: lowest''' irgendwo außerhalb von GrahamScan, und referenziere am niedrigsten mit '' 'GrahamScan :: niedrigstes '' '. – kaspersky

+0

Was meinst du "put Point2D GrahamScan :: niedrigsten irgendwo außerhalb GrahamScan"? – awbasham