图.h文件和.cpp文件我得到很多错误

时间:2013-01-18 06:00:51

标签: c++ graph

我有这段代码:

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

我收到了这个错误:

  

projects \ graphsearch \ graphsearch \ graph.cpp(21):错误C2664:'std :: vector&lt; _Ty&gt; :: push_back':无法将参数1从'Graph :: nodeT *'转换为'nodeT * const&amp; ;” ....

任何人都可以调试吗?

1 个答案:

答案 0 :(得分:1)

至少有三个错误。

首先,struct可以嵌套,并且您具有nodeTarcT的全局声明。它们位于全局命名空间中,因此您正在制作::nodeT*的向量,但真正的结构是Graph::nodeT*

将声明从顶部移动到private:下的结构内部。这是向前宣布它们的正确方法。

第二个是begin 可能会或可能不会不会返回arcT*,您应该使用auto

第三个是begin返回元素的迭代器,所以你需要取消引用它来获取指针,然后再取消引用它以获取成员,比如

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