初学C++ 一个简单程序求助
compile时会报错:undefined reference to `operator>>(std::istream&, Graph&)'
就这一个error,不懂怎么改啊!!
#ifndef GRAPH_H#define GRAPH_H#include <iostream>#include <vector>#include <cmath>#include <cassert>#include <algorithm>using namespace std;typedef vector<int> NLIST; // node (neighbor) listtypedef vector< NLIST > ADJLIST; // graph data structureclass Graph{private: ADJLIST adj; // adjacency lists.public: // creators + destroyers // Graph(int n) : adj(n) { } ~Graph() {} // stream I/0 // friend ostream& operator<<(ostream&, const Graph&); friend istream& operator>>(istream&, Graph&); // mutators // bool addArc(int u, int v) // false if already exists { if ( isArc(u,v) ) return false; adj[u].push_back(v); return true; } bool addEdge(int u, int v) // bi-directional arcs { return addArc(u,v) && addArc(v,u); } bool rmArc(int u, int v) // false if doesn't exist { int n = order(); assert(u >= 0 && u < n); assert(v >= 0 && v < n); assert(u != v); const NLIST::iterator it = find( adj[u].begin(), adj[u].end(), v ); if ( it==adj[u].end() ) return false; adj[u].erase(it); return true; } bool rmEdge(int u, int v) { return rmArc(u,v) && rmArc(v,u); } // accessors // int order() const { return adj.size(); } bool isArc(int u, int v) const { int n = order(); assert(u >= 0 && u < n); assert(v >= 0 && v < n); assert(u != v); NLIST::const_iterator it = find( adj[u].begin(), adj[u].end(), v ); if ( it==adj[u].end() ) return false; else return true; } bool isEdge(int u, int v) const { return isArc(u,v) && isArc(v,u); } // out-degree int degree(int u) const { return adj[u].size(); } const NLIST neighbors(int u) const { return (const NLIST&) adj[u]; }};#endifusing namespace std;typedef long long bigint;const bigint bigN = 0x3FFFFFFFLL << 32;bigint process(const Graph &R, int n, bigint cnt[], int v){ if (cnt[v]>=0) return cnt[v]; bigint maxin=0; // 0 means not reachable bigint ans=0; const NLIST N = R.neighbors(v); for (int i=0; i<N.size(); i++) { ans = process(R,n,cnt,N[i]); maxin = maxin + ans;assert(maxin < bigN); } return cnt[v]=maxin;}int main() { Graph G(1); while (true) { cin >> G; int n=G.order(); if (n==0) break; Graph R(n); // compute reverse graph from G for (int i=0; i<n; i++) { const NLIST N = G.neighbors(i); for (int j=0; j<N.size(); j++) { R.addArc(N[j],i); } } bigint cnt[n]; cnt[0]=1; for (int i=1; i<n; i++) cnt[i] = -1; // undetermined cout << process(R,n,cnt,n-1) << endl; }}
#ifndef GRAPH_H#define GRAPH_H#include <iostream>#include <vector>#include <cmath>#include <cassert>#include <algorithm>using namespace std;typedef vector<int> NLIST; // node (neighbor) listtypedef vector< NLIST > ADJLIST; // graph data structureclass Graph{private: ADJLIST adj; // adjacency lists.public: // creators + destroyers // Graph(int n) { adj.resize(n); int i; for(i=0;i<n;i++) { adj[i].resize(n); } } ~Graph() {} // stream I/0 // friend ostream& operator<<(ostream&, const Graph&); friend istream& operator>>(istream&, Graph&); // mutators // bool addArc(int u, int v) // false if already exists { if ( isArc(u,v) ) return false; adj[u].push_back(v); return true; } bool addEdge(int u, int v) // bi-directional arcs { return addArc(u,v) && addArc(v,u); } bool rmArc(int u, int v) // false if doesn't exist { int n = order(); assert(u >= 0 && u < n); assert(v >= 0 && v < n); assert(u != v); const NLIST::iterator it = find( adj[u].begin(), adj[u].end(), v ); if ( it==adj[u].end() ) return false; adj[u].erase(it); return true; } bool rmEdge(int u, int v) { return rmArc(u,v) && rmArc(v,u); } // accessors // int order() const { return adj.size(); } bool isArc(int u, int v) const { int n = order(); assert(u >= 0 && u < n); assert(v >= 0 && v < n); assert(u != v); NLIST::const_iterator it = find( adj[u].begin(), adj[u].end(), v ); if ( it==adj[u].end() ) return false; else return true; } bool isEdge(int u, int v) const { return isArc(u,v) && isArc(v,u); } // out-degree int degree(int u) const { return adj[u].size(); } const NLIST neighbors(int u) const { return (const NLIST&) adj[u]; }};istream& operator>>(istream& is,NLIST& nli) { cout << nli.size() << endl; int size=nli.size(); for(int i=0;i<size;i++) is >> nli[i]; return is;}istream& operator>>(istream& is, ADJLIST& adj) { int size=adj.size(); for(int i=0;i<size;i++) is >> adj[i]; return is; }istream& operator>>(istream& is, Graph& g) { return is>>g.adj;}#endifusing namespace std;typedef long long bigint;const bigint bigN = 0x3FFFFFFFLL << 32;bigint process(const Graph &R, int n, bigint cnt[], int v){ if (cnt[v]>=0) return cnt[v]; bigint maxin=0; // 0 means not reachable bigint ans=0; const NLIST N = R.neighbors(v); for (int i=0; i<N.size(); i++) { ans = process(R,n,cnt,N[i]); maxin = maxin + ans;assert(maxin < bigN); } return cnt[v]=maxin;}int main() { Graph G(1); while (true) { cin >> G; int n=G.order(); if (n==0) break; Graph R(n); // compute reverse graph from G for (int i=0; i<n; i++) { const NLIST N = G.neighbors(i); for (int j=0; j<N.size(); j++) { R.addArc(N[j],i); } } bigint cnt[n]; cnt[0]=1; for (int i=1; i<n; i++) cnt[i] = -1; // undetermined cout << process(R,n,cnt,n-1) << endl; }}
[解决办法]