foo(){
Network net(*this);
for(int u=0 ; u<Vertices.size() ; u++){ //for every vertex u
vector<Edges> NeighborsOfu = Vertices[u].adjacencyList; //tmp vector for edge delete
for(int v=0; v<Vertices[u].adjacencyList.size(); v++){ //for each neighbor v of u
int indexV = Vertices[u].adjacencyList[v].pointsTo; //index V = index of neighbor node v in Vertices
for(int w=0; w<Vertices[u].adjacencyList.size(); w++){ //for each neighbor of u, check if neighbor of
cout<<"topocontrol::INSIDE FIRST FOR (before b_search)\n";
//vindexOfW = index of node W in AL of node V, -1 if not found
//************************HERE IS WHERE IT BREAKS DOWN**********************//
int vIndexOfW = al_bSearch(Vertices[indexV].adjacencyList,
Vertices[u].adjacencyList[w].pointsTo); //search v's AL for w
//*************************UP THERE***************************************//
cout<<"After BSearch (before 2nd if) \n";
//if w was found in v's AL
if(vIndexOfW!= -1){ cout<< "INSIDE SECOND IF (if w is a common neighbor) \n"; //if u's neighbor w is found in v's AL
//check if weight of edge |uw| < |uv|
if(Vertices[u].adjacencyList[w].weight < Vertices[u].adjacencyList[v].weight){
//check if weight of edge |vw| < |uv|
cout<<"INSIDE THIRD IF (if |uw|<|uv|\n";
if(Vertices[indexV].adjacencyList[vIndexOfW].weight < Vertices[u].adjacencyList[v].weight){
cout<< "NOW ERASE \n";
int l = al_bSearch(NeighborsOfu, indexV); //to compensate for erase() moving remaining elements, must make sure you erase the correct one, whose index possibly changed in prev iterations
NeighborsOfu.erase(NeighborsOfu.begin()+l);
}
}
}
cout<<"after nested ifs\n";
}
}
cout<< "REPLACING NEIGHBOR LIST\n\n";
net.Vertices[u].adjacencyList = NeighborsOfu;
}
return net;
}
我正在尝试为分配实现网络拓扑控制协议,但随着节点数量的增加,程序开始在标题中给出错误,并附带两个十六进制内存地址。到目前为止,当二进制搜索完成并且尝试返回项目索引时,错误已经弹出(排序发生在代码中的其他地方)。我没有在代码中的任何地方使用free(),并且使用的唯一指针是复制构造函数(* this),而在main()中,wherer有一个指向对象实例的指针数组。我可以使用一些帮助找到它的原因,我已经在这几天仍然找不到它。
编辑:网络类继承了之前制作的myGraph类的大部分成员。
class wirelessNetwork : public myGraph {
public:
wirelessNetwork();
wirelessNetwork(float, int);
void setPosX(float, int);
void setPosY(float, int);
float vertexDistance(int, int);
wirelessNetwork topologyControl();
bool compassRouting(int, int, std::vector<int>& );
protected:
float size;
int n;
};
struct Edges{
float weight;
int pointsTo;
};
class Vertex{
public:
string data;
float posX;
float posY;
vector<Edges> adjacencyList;
Vertex() {};
Vertex(string st) { data = st;}
void setPosX(float x){posX = x;}
void setPosY(float y){posY = y;}
}; // end of Vertex struct
class myGraph{
public:
/recieve vertex, return vector containing pointsTo indexes
vector<int> getNeighborsOf(int);
//delete edges between 2 nodes
void deleteEdge(int, int);
//delete node and all connected edges
void deleteVertex(int);
int al_bSearch(vector<Edges>, int) const;
//get average node degree of graph. if parameter is received it leaves equal
float getAverageDegree();
float getAverageDegree(int&);
protected:
/***** Data Member *****/
vector<Vertex> Vertices;