0
struct Edge;

struct Node {
    int id;
    vector<Edge> *edges;
};

struct Edge {
    int id;
    Node* vertex1;
    Node* vertex2;
};

int main(){
    vector<Edge> nodeEdgeList;
    Node* nodePtr = new Node;
    nodePtr->id = vertex;
    nodePtr->edges = nodeEdgeList;
}

由于最后一行,我收到以下编译器错误。

error: cannot convert 'std::vector<Edge, std::allocator<Edge> >' to 'std::vector<Edge, std::allocator<Edge> >*' in assignment
4

2 回答 2

3

您的 Node 类有一个向量 * 作为您试图将非指针设置为指针的成员

错误信息在这里很清楚error: cannot convert 'std::vector<Edge, std::allocator<Edge> >' to 'std::vector<Edge, std::allocator<Edge> >*' in assignment

所以改为:

int main(){
    vector<Edge> *nodeEdgeList = new Vector<Edge>;
    Node* nodePtr = new Node;
    nodePtr->id = vertex;
    nodePtr->edges = nodeEdgeList;
}

或者

int main(){
    vector<Edge> nodeEdgeList;
    Node* nodePtr = new Node;
    nodePtr->id = vertex;
    nodePtr->edges = &nodeEdgeList;
}
于 2012-04-07T07:19:16.473 回答
2
int main(){
    vector<Edge> nodeEdgeList;
    Node* nodePtr = new Node;
    nodePtr->id = vertex;
    nodePtr->edges = &nodeEdgeList; // <-- use & before
}
于 2012-04-07T07:18:07.647 回答