指向类的指针的按位运算符?

时间:2014-12-29 20:45:13

标签: c++ bit-manipulation ifstream readfile

我花了一天的时间挖掘项目中前一个人的遗留代码,而且我也搜索过bitwise运算符我仍然无法清楚地理解按位运算符的代码行:

input >> *graph;

我可以编译并运行,但是,我为你的评论添加了2个printf函数:它可以打印" 11111"之后" 4",但永远不会打印" 2222",所以按位运算符行必定有问题。我该如何解决这个问题?

*graph是一个指向GGraph类对象的指针:

class GGraph{
public:
GGraph();

~GGraph();
void addNode ( GNodeData nodedata, GNodeOrGroup orgroup = GNOGROUP );
void delNode (); //code...........
};

仅供参考:这是在图表数据集(图表挖掘)中返回频繁模式的程序的一部分。我在这里要求的代码块只是一个从文件中打开和读取图形数据信息的过程。图表数据是这样的:

t # 0
v 0 0
v 1 0
...
(all the vertices with their labels)
e 0 1 3
e 1 2 3
...
(all the edges with the vetices they connect and their labels)
t # 1 (graph No.2)
....

这是程序在运行时无法传递的代码块:

void GDatabase::read ( string filename )
    {
        char header[100];
        ifstream input ( filename.c_str () );
        GGraph *graph = new GGraph ();

        input.getline ( header, 100 ); // we assume a header before each graph
        printf("%s", header);

    //    char c;
    //    c = input.get();
    //    while (input) {
    //        std::cout << c;
    //        c = input.get();}

        getchar();
        printf("11111111111");
        printf("\n%d",sizeof(graph));

        input >> *graph;
        printf("2222222222");
        while ( !input.eof () ) {
            process ( graph );
            graphs.push_back ( graph );
            graph = new GGraph ();
            input.getline ( header, 100 );
            input >> *graph;
        }
        delete graph;     
        input.close ();
    }

编辑:根据建议&#34;&gt;&gt;&#34;实际上是流提取器,我找到了运算符的这个定义:

istream& operator>>(istream& stream, GGraph &graph )
{
  char m;
  GNodeData nd;
  GEdgeData ed;
  GNodeOrGroup og;
  GNodeID ni, ni1, ni2;

  m = stream.get ();

  if ( stream.eof () )
    return stream;

  while ( !stream.eof () && m == 'v' ) {
    stream >> ni;
    stream >> nd;
    stream >> og;
    graph.addNode ( nd, og );
    do {
      m = stream.get (); // get end of line
    } 
    while ( m != '\n' ); 
    m = stream.get ();
  }
  while ( !stream.eof () && m == 'e' ) {
    stream >> ni1;
    stream >> ni2;
    stream >> ed;
    graph.addEdge ( ni1, ni2, ed );
    do {
      m = stream.get (); // get end of line
    }
    while ( m != '\n' ); 
    m = stream.get ();
  }

  stream.unget (); 
  stream.clear (); // also unput eof if we read 

  return stream;
}

1 个答案:

答案 0 :(得分:2)

  

输入&gt;&gt; *图;

这不是一个按位运算符。它是一个流提取操作符。代码中的某处,必须定义一个operator>>,它将流和GGraph作为输入,例如:

template<class CharT, class Traits = std::char_traits<CharT> >
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits> &in, GGraph &graph)
{
    // read values from in and store them in graph as needed...
    return in;
}

所以这一行:

input >> *graph;

实际上在呼唤:

operator>>(input, *graph);