')'令牌c ++之前的预期类型说明符

时间:2014-04-28 21:34:03

标签: c++

我正在尝试重载“<<”操作员打印出我制作的结构。该结构是一个散列数组树。我有一个“HashNode”类,它在哈希树的节点上执行所有操作,并且我在类中重载运算符。我有一个MLH类,它实例化一个HashNode。以下是相关代码:

HashNode.cpp

template <typename T>
ostream &operator << ( ostream &output, const HashNode<T> &node ) {
  if(node.stemNode == 0){
    //leaf node                                                                               
    for(int i=0; i < node.numElements; i++) {
      output << "\n-------------------------------------------" << endl;
      output << "This Node's Level: " << node.nodeLevel << endl;
      output << "\nKey: " << node.keyArray[i] << endl;
      output << "\nData: " << *(node.dataArray[i])  << endl;
    }
    output << "\n\n";
  }else if (node.stemNode == 1) {
    //stem node                                                                               
    for(int j=0; j<5; j++) {
      if(node.childArray[j] != NULL) {
        output << *(node.childArray[j]);
        // levelCtr++;                                                                   
      }
    }
  }
  int lowest;
  int ctr = 0;
  for(int i = 8; i>=0; i--) {
    if(levels[i]==0) {
      ctr++;
    }
  }
  lowest = 9-(ctr+1);
  if(node.nodeLevel == 0) {
    output << "\n\nLowest Level: " << (9-(ctr+1)) << endl;
  }
  return output; //<< "\n\nLowest Level: " << lowest <<   endl;                                   
}

MLH.cpp

template <typename T>
ostream &operator << (ostream &output, const MLH<T> &tree)
{
  //HashNode<T> ptr = tree;                                                       
  output << *(tree->proot->operator);
  return output;
}

出于某种原因,我在尝试编译时遇到以下错误:

g++ -c -g Hashtest.cpp
In file included from Hashtest.cpp:2:0:
MLH.h: In function ‘std::ostream& operator<<(std::ostream&, const MLH<T>&)’:
MLH.h:69:36: error: expected type-specifier before ‘)’ token
output << *(tree->proot->operator);
                             ^
make: *** [Hashtest.o] Error 1

如果我将输出流从HashNode类返回到MLH类中的输出流,为什么会出现问题?感谢您的帮助,我为任何格式问题道歉。

编辑:我应该指定“proot”是MLH对象的属性。它是指向HashNode对象的指针。通过说“tree-&gt; proot-&gt;运算符”,我试图在HashNode类中调用重载运算符函数。

1 个答案:

答案 0 :(得分:1)

output << *(tree->proot->operator);

这不是如何调用重载的&lt;&lt;运营商。这是如何:

template <typename T>
ostream &operator << (ostream &output, const MLH<T> &tree)
{                                               
    output << *(tree->proot);  // Assuming tree->proot is a HashNode<T> pointer
    return output;
}