首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

初学C++ 一个简单程序

2012-06-05 
初学C++ 一个简单程序求助compile时会报错:undefined reference to `operator(std::istream&, Graph&)

初学C++ 一个简单程序求助
compile时会报错:undefined reference to `operator>>(std::istream&, Graph&)'
就这一个error,不懂怎么改啊!!

C/C++ code
#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;    }}



[解决办法]
给你的Graph 类重载输入运算符>>吧。
[解决办法]
这个>>操作符不是C++默认的。

详细看这个把。
http://bbs.bccn.net/thread-327594-1-1.html
[解决办法]
是不是因为没有写实现?
[解决办法]
重载已经写好了
构造函数重新写了一些,输入的时候必须考虑vector的大小,所以在构造是vector也初始化了一下
但是逻辑上似乎还有问题,表示太花时间了....
C/C++ code
#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;    }} 


[解决办法]

探讨
compile时会报错:undefined reference to `operator>>(std::istream&amp;, Graph&amp;)'
就这一个error,不懂怎么改啊!!

C/C++ code

#ifndef GRAPH_H
#define GRAPH_H

#include <iostream>
#include <vector>
#include <cmat……

热点排行