简体   繁体   中英

Graph .h file and .cpp file im getting many errors

I have this code:

using namespace std;
struct nodeT;
struct arcT;


class Graph
{
public:

Graph(string xd);

    void addnode(string name,float xval,float yval);
    void addarc(string n1,string n2,float dist);
    void printarcs();
private:
    struct graphT
    {
        vector<nodeT *>nodes;
        vector<arcT * > arcs;
        map<string,nodeT * > nodemap;
    };
    struct nodeT{
        string nodename;
        float x,y;
        vector<arcT * > arcs;
    };
    struct arcT{
        nodeT * start;
        nodeT * finish;
        float distance;
    };
    void arcfinal(nodeT * a,nodeT * b, float len);
    graphT * g;
    //graphT *g=new graphT;
};

//#include "BST.cpp"

#endif



#include "Graph.h"


Graph::Graph(string xd)
{
g=new graphT;

}

void Graph::addnode(string name,float xval,float yval)
{
    //if(!nodemap[name])

    nodeT *t=new nodeT;
    t->nodename=name;
    t->x=xval;
    t->y=yval;


    g->nodes.push_back(t);
    g->nodemap[name]=t;

}

void Graph::addarc(string n1,string n2,float dist)
{
    nodeT * t1=g->nodemap[n1];
    nodeT * t2=g->nodemap[n2];

    arcfinal(t1,t2,dist);
    arcfinal(t2,t1,dist);

}

void Graph::arcfinal(nodeT * a,nodeT * b, float len)
{
    arcT * d=new arcT;
    d->start=a;
    d->finish=b;
    d->distance=len;

    g->arcs.push_back(d);
    a->arcs.push_back(d);
}

void Graph::printarcs()
{  
    for(arcT * curr=g->arcs.begin();curr != g->arcs.end();++curr)
    {
        cout<<curr->start->nodename<<"-----"<<curr->finish->nodename<<"----"<<curr->distance<<endl;
    }
}

//#endif

And I am getting this error:

projects\\graphsearch\\graphsearch\\graph.cpp(21) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'Graph::nodeT *' to 'nodeT *const &'....

Can anyone please debug it?

There are at least three errors.

The first is that struct s can be nested, and you have global declarations of nodeT and arcT . They are in the global namespace, so you're making vectors of ::nodeT* , but the real structs are Graph::nodeT* .

Move the declarations from the top to inside the struct under the private: . That's the proper way to forward-declare them.

The second is that begin may or may not does not return an arcT* , you should use auto .

The third is that begin returns an iterator to the element, so you need to dereference it to get to the pointer, then dereference it again to get to members, like

cout << (*curr)->start->nodename << "-----" << (*curr)->finish->nodename << "----" << (*curr)->distance << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM