无法声明 2 位好友超载<<在模板 .h 中

发布于 2024-10-06 21:36:30 字数 1632 浏览 7 评论 0 原文

我试图在模板 BSTree.h 中创建两个重载运算符,但遇到的错误实际上并没有告诉我问题是什么。单独或结合对错误代码进行搜索对我来说没有产生任何结果。

第一个重载<<对于 BSTree 不会在编译时导致任何错误,但第二个重载<<我为 Node 创建的结构不断返回以下错误:

错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持default-int
错误 C2143: 语法错误 : 在 '*' 之前缺少 ','

#ifndef BSTREE_H
#define BSTREE_H

#include <iostream>
#include <fstream>

template <typename T>
class BSTree{

friend ostream& operator<<(ostream&, const BSTree<T>&); 

public:
    BSTree();
    //BSTree(const BSTree &);
    ~BSTree();

    void buildTree(ifstream&);
    void setType(char);
    bool getType(char);

    bool insert(T*);

    bool isEmpty();


private:
    char type;

    struct Node{
        T* data;

        //subnode[0] == left subtree
        //subnode[1] == right subtree
        Node* subnode[2];
    };

    Node* head;
    void destructorHelper(Node* &);
    bool insertHelper(T*, Node* &);
    friend ostream& operator<<(ostream&, const Node*&); 

};

编译器表示错误发生在 Node 重载的行<<代码是。

template <typename T>
ostream& operator<<(ostream &output, const BSTree<T> &out) {
    if(head != NULL)
        output << head;
    return output;
}

template <typename T>
ostream& operator<<(ostream &output, const Node* &out) {
    if(out != NULL){
        output << out->subnode[0];
        output << *out->data;
        output << out->subnode[1];
    }

    return output;
}  

难道我不可以声明2超载<<在同一个 .h 中,即使它们用于不同的对象?或者我的代码搞砸了?

I'm trying to create two overloaded operators in a template BSTree.h and am encountering errors that really don't tell me what the problem is. Running a search on the error codes seperate or in conjunction hasn't yielded anything for me.

The first overloaded<< for the BSTree doesn't cause any errors on compile, but the 2nd overloaded<< I created for my Node struct keeps returning the following errors:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '*'

#ifndef BSTREE_H
#define BSTREE_H

#include <iostream>
#include <fstream>

template <typename T>
class BSTree{

friend ostream& operator<<(ostream&, const BSTree<T>&); 

public:
    BSTree();
    //BSTree(const BSTree &);
    ~BSTree();

    void buildTree(ifstream&);
    void setType(char);
    bool getType(char);

    bool insert(T*);

    bool isEmpty();


private:
    char type;

    struct Node{
        T* data;

        //subnode[0] == left subtree
        //subnode[1] == right subtree
        Node* subnode[2];
    };

    Node* head;
    void destructorHelper(Node* &);
    bool insertHelper(T*, Node* &);
    friend ostream& operator<<(ostream&, const Node*&); 

};

The compiler says the errors occur at the line where the Node overloaded<< code is.

template <typename T>
ostream& operator<<(ostream &output, const BSTree<T> &out) {
    if(head != NULL)
        output << head;
    return output;
}

template <typename T>
ostream& operator<<(ostream &output, const Node* &out) {
    if(out != NULL){
        output << out->subnode[0];
        output << *out->data;
        output << out->subnode[1];
    }

    return output;
}  

Am I not allowed to declare 2 overloaded<< in the same .h even if they are for different objects? Or am I messing something up in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉怎暖 2024-10-13 21:36:30

错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持default-int

通常这意味着编译器不知道标识符作为类型,因此它假设它是一个参数名称,并且类型隐式为 int >。 (在旧的 C 中,有一条规则可以省略参数类型中的 int。)

void foo(bar);

如果编译器不知道类型 bar,类似的代码可能会发出此消息> 并假设void foo(int bar)

template <typename T>
std::ostream& operator<<(std::ostream &output, const typename BSTree<T>::Node* &out)
{
  // ...
}

应该可以编译。 (请注意限定条件 std::BSTree::。)

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Usually this means that the compiler doesn't know an identifier as a type, so it goes assuming it's a parameter name, with the type implicitly being int. (In C of old there was a rule that the int in a parameter type could be omitted.) Code like

void foo(bar);

might emit this, if the compiler doesn't know the type bar and assumes void foo(int bar).

This

template <typename T>
std::ostream& operator<<(std::ostream &output, const typename BSTree<T>::Node* &out)
{
  // ...
}

should compile. (Note the qualifications std:: and BSTree::.)

隔纱相望 2024-10-13 21:36:30

您的代码中有几个错误:

  • 您需要添加 <>在声明为友元的函数名称之后,
  • 即使您位于友元函数的主体中,也需要指定调用其方法的对象。请记住,函数不绑定到实例。
  • 在最后一个原型中,Node 在定义它的类之外是不可访问的。您必须精确 BSTree::Node

You've got several mistakes in your code:

  • you need to add <> after the function names you declare as friend
  • even if you're in the body of a friend a function, you need to specify objects you call methods on. Remember that functions are not bound to an instance.
  • in the last prototype, Node is not reachable outside the class it's defined in. You must precise BSTree::Node
我的鱼塘能养鲲 2024-10-13 21:36:30

可能您需要这个:

const BSTree::Node* &out

Node 是内部结构。

Possibly you need this:

const BSTree::Node* &out

Node is internal structure.

暮年慕年 2024-10-13 21:36:30

我怀疑问题在于,在声明您的 operator<<() 友元函数时,ostream 不在范围内。

要么在 class BSTree{ 内添加 using std::ostream;,要么指定完全限定的类型名:

friend std::ostream& operator<<(std::ostream&, const BSTree<T>&); 
...
friend std::ostream& operator<<(std::ostream&, const Node*&);

无论哪种方式,这些函数的实际定义都需要进行类似的更改。无论你做什么,不要试图在 a 中使用 using std::ostream; 或(更糟糕)using namespace std;文件范围内的头文件,因为它将影响翻译单元中的每个后续声明。

I suspect the problem is that ostream is not in scope at the time that your operator<<() friend functions are declared.

Either add using std::ostream; just inside class BSTree{, or specify the fully qualified typenames:

friend std::ostream& operator<<(std::ostream&, const BSTree<T>&); 
...
friend std::ostream& operator<<(std::ostream&, const Node*&);

Either way, the actual definitions of these functions will need to be changed similarly. Whatever you do, don't be tempted to use either using std::ostream; or (even worse) using namespace std; in a header file at file scope, because it will affect every subsequent declaration in the translation unit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文