3

I'm still kind of new to C++ and I just can't figure out how to fix the error. Error I'm getings is saying "Invalid operands to binary expression". Does anyone have any idea on how to fix this? Any help is appreciated!! I'm using Xcode 4.5.2. Thanks!

[Edit] OK. I've updated the code below. I'm still getting the same error at the same line (find())... but here are the changes...

Vertex myVertex = new Vertex(1); 

to

Vertex myVertex(1);

And added operator overload for <

struct Vertex {
    int node;
    bool known;
    float distance;
    Vertex *path;

    Vertex(int n=0, bool known=false, duuble dist=INFINITY):
    node(n), known(known), distance(dist) {}
};


struct Edge {
    Vertex target;
    float weight;
    bool directed;

    Edge(Vertex v2, float w, bool dir):target(v2), weight(w), directed(dir) {}
};

class Graph {
public:
    /*unrelated members are removed*/

    void findVertex() {
        Vertex myVertex(1);
        map<Vertex, vector<Edge> >::iterator itr;
        itr = edges.find(myVertex); // <--- this is the line the error seems to related to
    }

    friend bool operator< (const Vertex& lhs, const Vertex& rhs) {
        return lhs.node < rhs.node;
    }



private:
    /*unrelated members are removed*/
    vector<Vertex> vertices;
    map<Vertex, vector<Edge> > edges;
};
selina
  • 71
  • 1
  • 2
  • 5

3 Answers3

3

The elements in the map are sorted from lower to higher key value following a specific strict weak ordering criterion set on construction.

You need to provide operator< for Vertex, for example:

bool operator<(const Vertex& lhs, const Vertex& rhs){
      return lhs.node < rhs.node;
  }

Also Vertex myVertex = *new Vertex(1); leaks memory as David and selbie mentioned. Have a look at this link: Why does the use of 'new' cause memory leaks?

change Vertex myVertex = *new Vertex(1); to Vertex myVertex = Vertex(1);

Community
  • 1
  • 1
billz
  • 44,644
  • 9
  • 83
  • 100
  • I conceptually understand the importance of `new` and `delete`. but I'm getting and error (yes, additional error) for `delete myVertex`. The error says, "Cannot delete expression Vertex". Why can I not delete a `new`ed object? – selina Dec 16 '12 at 04:06
  • I had `bool operator<(....)` in a wrong place. As soon as I moved it to `struct Vertex {....};`, the error disappeared! thanks for pointing me to the right direction! – selina Dec 16 '12 at 04:24
1

I have the same problem, finally, I just change the Xcode default C++ library to libstdc++(GNU C++ standard library) in the Build Settings. And it's solved.

Just like the picture below. enter image description here

SamSam
  • 113
  • 8
0

I like BillZ's answer. But I would never store duplicate data. Such is the case with storing a duplicate Vertex member between the vertices and edges member.

Consider doing this, and then you don't have implement a comparison operator in the Vertex class. I didn't know what your findVertex function would take as an input parameter, so I just wrote it both ways.

class Graph {
public:
    /*unrelated members are removed*/

    void findVertex(int nodeNumber) {
        map<int, vector<Edge> >::iterator itr;
        itr = edges.find(nodeNumber);
    }

    void findVertex(const Vertex& v) {
        map<int, vector<Edge> >::iterator itr;
        itr = edges.find(v.node);
    }

private:
    /*unrelated members are removed*/
    vector<Vertex> vertices;
    map<int, vector<Edge> > edges;
};
selbie
  • 100,020
  • 15
  • 103
  • 173
  • My plan was to have the first element of edges to be Vertex and the second to be vector of Edge, hence my implementation of edges was: map > edges; so my find operation on edges was map >::iterator itr; itr = edges.find( myVertex ); I'm trying out Billz's solution but I'm still getting the same error. I've added friend bool operator< (const Vertex& lhs, const Vertex& rhs) { return lhs.node < rhs.node; } inside of Graph class. – selina Dec 16 '12 at 03:44