空リストの配列を作成し、main()およびGraph()コンストラクタ内からこれらのリストの位置を出力しようとしました。しかし、彼らは一致しません!助けてください!リストの配列 - コンストラクタ内のアドレスとmain()内のアドレスが一致しません
#include<iostream>
#include<list> // use list in STL
using namespace std;
struct Edge {
int v, w;
Edge(int v = -1, int w = 0) : v(v), w(w){}
};
class Graph {
private:
const int V; //number of vertices
int E; //number of edges
list<Edge>* adj[];//adjacency list
public:
Graph(int N):V(N){ //create a graph with no edges
E = 0; list<Edge>* adj[V];
for(int i = 0; i < N; i++){
adj[i] = new list<Edge>();
cout << "at " << i <<" address "<< adj[i] <<"\n";
}
}
friend ostream& operator<<(ostream& out, const Graph& G);
};
ostream& operator<<(ostream& out, const Graph& G){
out << "Vertices: "<< G.V <<" and Edges: "<< G.E <<"\n" << "The adjacency list:\n";
for(int i = 0; i < G.V; i++){
out << "vertex "<< i << ":"<< G.adj[i] <<" -> zero\n";
}
return out;
}
int main() {
Graph* G = new Graph(50);
cout << *G;
}
この定義で何を期待していますか?リスト * adj []; '? –
Amadeus
'E = 0;リスト * adj [V]; 'ここでローカル変数を作成しています。 –
出力を含めることができませんでした。 1行に2つのステートメントがあるコードがあります。これはバグがある行です。あなたのメソッドはクラスメンバをシャドウし、コンパイラに '-Wall'を渡すことで、あなたが間違っていたことを警告してくれました。 – Yakk