代码之家  ›  专栏  ›  技术社区  ›  user2754048

Ostrem身份不明?

  •  0
  • user2754048  · 技术社区  · 11 年前

    所以我有一个头文件,它有两个使用ostream的函数 并且我试图重载间接运算符(<<),以允许我使用指向模板化列表节点的指针写入文件。

    来自.h文件的是原型

    void PrintForward(ostream &out);
    void PrintBackward(ostream &out);
    ostream& operator<< (ostream &out, List<t> const* p);
    

    然后从.cpp文件

    运算符重载功能

    ostream& operator<< (ostream &out, ListNode::List const* p)
    {
        return out << *p;
    }
    

    打印转发功能

    template <typename T>
    void List<T>::PrintForward(ostream &out)
    {
        ListNode* lp = head;
    
    while(lp != NULL)
    {
        out << *lp->value;
        lp = lp -> next;
    }
    }
    

    向后打印功能

    template <typename T>
    void List<T>::PrintBackward(ostream &out)
    {
        ListNode* lp = tail;
    
        while(lp != NULL)
        {
            out << *lp;
            lp = lp -> prev;
        }
    }
    

    目前我得到的只是一个编译器错误

    error C2061: syntax error : identifier 'ostream' 
    

    但我找不到。在我将所有函数切换到.cpp文件之前,我收到了一个不同的错误,说明类模板的使用需要模板参数列表。但它似乎已经消失了。

    2 回复  |  直到 11 年前
        1
  •  0
  •   hidrargyro    11 年前

    我可以在这个代码中看到许多问题: 您对操作员的声明<&书信电报;正在使用模板类t,但没有该模板类的声明,如

    template<class t>
    ostream& operator<< (ostream &out, List<t> const* p);
    

    此外,这些函数的声明和定义在第二个参数中也不相等:

    ostream& operator<< (ostream &out, List<t> const* p);
    ostream& operator<< (ostream &out, ListNode::List const* p)
    

    最后,我不知道你的代码中是否使用了命名空间std,如果不是,那么你必须在ostream类之前写std:,如下所示:

    std::ostream& operator<< (std::ostream &out, List<t> const* p);
    std::ostream& operator<< (std::ostream &out, ListNode::List const* p)
    
        2
  •  0
  •   Werner Erasmus    11 年前

    你还没有发布所有的代码,但我注意到你没有用std来限定ostream:

    //.h
    std::ostream& operator<< (std::ostream &out, List<t> const* p);
    
    //.cpp
    //...
    // Either qualify with std, or bring it into scope by using namespace std...