奥流身份不明

Ostream unidentified?

本文关键字:身份      更新时间:2023-10-16

所以我有这个头文件,它有 2 个利用 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文件之前,我收到一个不同的错误,指出使用类模板需要模板参数列表。但它似乎已经消失了。

我可以在这段代码中看到很多问题:运算符<<的声明使用的是模板类 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)

您尚未发布所有代码,但我注意到您没有使用 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...