C++ C++;:操作员<&书信电报;未能编译

C++ C++;:操作员<&书信电报;未能编译,c++,templates,friend,C++,Templates,Friend,我在C++中编码一个B树,但是当我超载操作程序叶子> { 返回{nullptr,0}; }否则{ 返回搜索(x->child[i].get(),k); } } void SplitChild(节点*x,标准::大小\u t i){ 如果(!x){ 返回; } 自动y=x->child[i].get(); 如果(!y){ 返回; } 断言(!x->isFull()&&y->isFull()); auto z=std::make_unique(); z->leaf=y->leaf; z->setN(

我在C++中编码一个B树,但是当我超载<代码>操作程序叶子> { 返回{nullptr,0}; }否则{ 返回搜索(x->child[i].get(),k); } } void SplitChild(节点*x,标准::大小\u t i){ 如果(!x){ 返回; } 自动y=x->child[i].get(); 如果(!y){ 返回; } 断言(!x->isFull()&&y->isFull()); auto z=std::make_unique(); z->leaf=y->leaf; z->setN(t-1); 对于(std::size_t j=0;j键[j]=y->键[j+t]; } 如果(!y->leaf){ 对于(std::size_t j=0;jchild[j]=std::move(y->child[j+t]); } } x->setN(x->getN()+1); 对于(std::size_t j=x->getN()+1;j>=i+1;j--){ x->child[j+1]=std::move(x->child[j]); } x->child[i+1]=std::move(z); 对于(std::size_t j=x->getN()+1;j>=i+1;j--){ x->key[j]=x->key[j-1]; } x->键[i]=y->键[t]; y->setN(t-1); } void InsertNonFull(节点*x,常数T&k){ std::size_t i=x->getN(); 如果(x->叶){ x->setN(i+1); 而(igetN()&&kkey[i]){ x->key[i+1]=x->key[i]; 我--; } x->键[i+1]=k; }否则{ 而(igetN()&&kkey[i]){ 我--; } i++; 如果(x->child[i]->isFull()){ SplitChild(x,i); 如果(k>x->键[i]){ i++; } } InsertNonFull(x->child[i].get(),k); } } 公众: b树(){ root=std::使_唯一(); } [[nodiscard]]std::成对搜索(const T&k)const{ 返回搜索(root.get(),k); } 无效插入(常数T&k){ 如果(root->isFull()){ 自动s=std::使_唯一(); s->leaf=false; s->setN(0); s->child[0]=std::move(root); 根=标准::移动; SplitChild(root.get(),0); InsertNonFull(root.get(),k); }否则{ InsertNonFull(root.get(),k); } }
朋友STD::Orths&操作员

代码>运算符> P>哇,C++模板是如此晦涩和愚蠢。 工作代码:

#include <cassert>
#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>
#include <utility>

template <typename T, std::size_t t>
class BTree {
    static_assert(t >= 2);
    class Node {
        std::size_t n = 0;
    public:
        bool leaf = true;
        std::vector<T> key;
        std::vector<std::unique_ptr<Node>> child;
        void setN(std::size_t N) {
            n = N;
            key.resize(n);
            if (!leaf) {
                child.resize(n + 1);
            }
        }

        [[nodiscard]] std::size_t getN() const {
            return n;
        }

        [[nodiscard]] bool isFull() const {
            return n == 2 * t - 1;
        }

        friend std::ostream& operator<<(std::ostream& os, const BTree<T, t>::Node& node) {
            if (node.leaf) {
                for (std::size_t i = 0; i < node.getN() - 1; i++) {
                    os << node.key[i] << ' ';
                }
                os << node.key[node.getN() - 1];
            } else {
                for (std::size_t i = 0; i < node.getN(); i++) {
                    os << *node.child[i] << ' ' << node.key[i] << ' ';
                }
                os << *node.child[node.getN()];
            }
            return os;
        }
    };

    std::unique_ptr<Node> root;

    std::pair<const Node*, std::size_t> Search(const Node* x, const T& k) const {
        std::size_t i = 0;
        while (i < x->getN() && k > x->key[i]) {
            i++;
        }
        if (i < x->getN() && k == x->key[i]) {
            return {x, i};
        } else if (x->leaf) {
            return {nullptr, 0};
        } else {
            return Search(x->child[i].get(), k);
        }
    }

    void SplitChild(Node* x, std::size_t i) {
        if (!x) {
            return;
        }
        auto y = x->child[i].get();
        if (!y) {
            return;
        }
        assert(!x->isFull() && y->isFull());
        auto z = std::make_unique<Node>();
        z->leaf = y->leaf;
        z->setN(t - 1);
        for (std::size_t j = 0; j < t - 1; j++) {
            z->key[j] = y->key[j + t];
        }
        if (!y->leaf) {
            for (std::size_t j = 0; j < t; j++) {
                z->child[j] = std::move(y->child[j + t]);
            }
        }
        x->setN(x->getN() + 1);
        for (std::size_t j = x->getN(); j >= i && j < x->getN(); j--) {
            x->child[j + 1] = std::move(x->child[j]);
        }
        x->child[i + 1] = std::move(z);
        for (std::size_t j = x->getN(); j >= i && j < x->getN(); j--) {
            x->key[j] = x->key[j - 1];
        }
        x->key[i] = y->key[t - 1];
        y->setN(t - 1);
    }

    void InsertNonFull(Node* x, const T& k) {
        std::size_t i = x->getN();
        if (x->leaf) {
            x->setN(i + 1);
            while (i - 1 < x->getN() && k < x->key[i - 1]) {
                x->key[i] = x->key[i - 1];
                i--;
            }
            x->key[i] = k;
        } else {
            while (i - 1 < x->getN() && k < x->key[i - 1]) {
                i--;
            }
            if (x->child[i]->isFull()) {
                SplitChild(x, i);
                if (k > x->key[i]) {
                    i++;
                }
            }
            InsertNonFull(x->child[i].get(), k);
        }

    }

public:
    BTree() {
        root = std::make_unique<Node>();
    }

    [[nodiscard]] std::pair<const Node*, std::size_t> Search(const T& k) const {
        return Search(root.get(), k);
    }

    void Insert(const T& k) {
        if (root->isFull()) {
            auto s = std::make_unique<Node>();
            s->leaf = false;
            s->setN(0);
            s->child[0] = std::move(root);
            root = std::move(s);
            SplitChild(root.get(), 0);
            InsertNonFull(root.get(), k);
        } else {
            InsertNonFull(root.get(), k);
        }
    }

    friend std::ostream& operator<< (std::ostream& os, const BTree<T, t>& bt) {
        return os << *(bt.root) << '\n';
    }

};

int main() {
    BTree<int, 2> tree;
    tree.Insert(1);
    tree.Insert(2);
    tree.Insert(3);
    tree.Insert(4);
    tree.Insert(5);
    tree.Insert(6);
    tree.Insert(7);
    tree.Insert(8);
    tree.Insert(9);
    tree.Insert(10);

    std::cout << tree;

}
#包括
#包括
#包括
#包括
#包括
#包括
模板
B类树{
静态断言(t>=2);
类节点{
标准:尺寸n=0;
公众:
布尔叶=真;
std::向量键;
性病:病媒儿童;
无效设置(标准::大小\u t N){
n=n;
键。调整大小(n);
如果(!叶){
调整大小(n+1);
}
}
[[nodiscard]]std::size\u t getN()常量{
返回n;
}
[[nodiscard]]bool isFull()常量{
返回n==2*t-1;
}
friend std::ostream&operatorisFull());
auto z=std::make_unique();
z->leaf=y->leaf;
z->setN(t-1);
对于(std::size_t j=0;j键[j]=y->键[j+t];
}
如果(!y->leaf){
对于(std::size_t j=0;jchild[j]=std::move(y->child[j+t]);
}
}
x->setN(x->getN()+1);
对于(std::size_t j=x->getN();j>=i&&jgetN();j--){
x->child[j+1]=std::move(x->child[j]);
}
x->child[i+1]=std::move(z);
对于(std::size_t j=x->getN();j>=i&&jgetN();j--){
x->key[j]=x->key[j-1];
}
x->键[i]=y->键[t-1];
y->setN(t-1);
}
void InsertNonFull(节点*x,常数T&k){
std::size_t i=x->getN();
如果(x->叶){
x->setN(i+1);
而(i-1getN()&&kkey[i-1]){
x->key[i]=x->key[i-1];
我--;
}
x->键[i]=k;
}否则{
而(i-1getN()&&kkey[i-1]){
我--;
}
如果(x->child[i]->isFull()){
SplitChild(x,i);
如果(k>x->键[i]){
i++;
}
}
InsertNonFull(x->child[i].get(),k);
}
}
公众:
b树(){
root=std::使_唯一();
}
[[nodiscard]]std::成对搜索(const T&k)const{
返回搜索(root.get(),k);
}
无效插入(常数T&k){
如果(root->isFull()){
自动s=std::使_唯一();
s->leaf=false;
s->setN(0);
s->child[0]=std::move(root);
根=标准::移动;
SplitChild(root.get(),0);
InsertNonFull(root.get(),k);
}否则{
InsertNonFull(root.get(),k);
}
}

friend std::ostream&Operator我认为不能推断类型名BTree::Node中的T和T。我更喜欢提供公共成员函数
无效打印(std::ostream&)const;
std::ostream&operator@Eljay但是我团队的高级程序员告诉我,使用
print
而不是
操作符是不好的做法。你可以
inline
操作符,因为节点的每个实例化都会创建自己的友元函数(不是模板)编译器可以很容易地查看所有的friend函数,看看哪一个适合;它不需要猜测任何模板参数。
/mnt/c/Users/kim/CLionProjects/PPP/main.cpp: In instantiation of ��std::ostream& operator<<(std::ostream&, const BTree<T, t>&) [with T = int; long unsigned int t = 2; std::ostream = std::basic_ostream<char>]��:
/mnt/c/Users/kim/CLionProjects/PPP/main.cpp:171:18:   required from here
/mnt/c/Users/kim/CLionProjects/PPP/main.cpp:164:8: error: no match for ��operator<<�� (operand types are ��std::ostream�� {aka ��std::basic_ostream<char>��} and ��BTree<int, 2>::Node��)
  164 |     os << *(bt.root) << '\n';
      |     ~~~^~~~~~~~~~~~~
template <typename T, std::size_t t>
class BTree {
    ...
    class Node {
        ...
        friend std::ostream& operator<< (std::ostream& os, const Node& node) {
            if (node->leaf) {
                for (std::size_t i = 0; i < node->getN() - 1; i++) {
                    os << node->key[i] << ' ';
                }
                os << node->key[node->getN() - 1];
            } else {
                for (std::size_t i = 0; i < node->getN(); i++) {
                    os << *node->child[i] << ' ' << node->key[i] << ' ';
                }
                os << *node->child[node->getN()];
            }
            return os;
        }
        ...
    };
    ...
};
#include <cassert>
#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>
#include <utility>

template <typename T, std::size_t t>
class BTree {
    static_assert(t >= 2);
    class Node {
        std::size_t n = 0;
    public:
        bool leaf = true;
        std::vector<T> key;
        std::vector<std::unique_ptr<Node>> child;
        void setN(std::size_t N) {
            n = N;
            key.resize(n);
            if (!leaf) {
                child.resize(n + 1);
            }
        }

        [[nodiscard]] std::size_t getN() const {
            return n;
        }

        [[nodiscard]] bool isFull() const {
            return n == 2 * t - 1;
        }

        friend std::ostream& operator<<(std::ostream& os, const BTree<T, t>::Node& node) {
            if (node.leaf) {
                for (std::size_t i = 0; i < node.getN() - 1; i++) {
                    os << node.key[i] << ' ';
                }
                os << node.key[node.getN() - 1];
            } else {
                for (std::size_t i = 0; i < node.getN(); i++) {
                    os << *node.child[i] << ' ' << node.key[i] << ' ';
                }
                os << *node.child[node.getN()];
            }
            return os;
        }
    };

    std::unique_ptr<Node> root;

    std::pair<const Node*, std::size_t> Search(const Node* x, const T& k) const {
        std::size_t i = 0;
        while (i < x->getN() && k > x->key[i]) {
            i++;
        }
        if (i < x->getN() && k == x->key[i]) {
            return {x, i};
        } else if (x->leaf) {
            return {nullptr, 0};
        } else {
            return Search(x->child[i].get(), k);
        }
    }

    void SplitChild(Node* x, std::size_t i) {
        if (!x) {
            return;
        }
        auto y = x->child[i].get();
        if (!y) {
            return;
        }
        assert(!x->isFull() && y->isFull());
        auto z = std::make_unique<Node>();
        z->leaf = y->leaf;
        z->setN(t - 1);
        for (std::size_t j = 0; j < t - 1; j++) {
            z->key[j] = y->key[j + t];
        }
        if (!y->leaf) {
            for (std::size_t j = 0; j < t; j++) {
                z->child[j] = std::move(y->child[j + t]);
            }
        }
        x->setN(x->getN() + 1);
        for (std::size_t j = x->getN(); j >= i && j < x->getN(); j--) {
            x->child[j + 1] = std::move(x->child[j]);
        }
        x->child[i + 1] = std::move(z);
        for (std::size_t j = x->getN(); j >= i && j < x->getN(); j--) {
            x->key[j] = x->key[j - 1];
        }
        x->key[i] = y->key[t - 1];
        y->setN(t - 1);
    }

    void InsertNonFull(Node* x, const T& k) {
        std::size_t i = x->getN();
        if (x->leaf) {
            x->setN(i + 1);
            while (i - 1 < x->getN() && k < x->key[i - 1]) {
                x->key[i] = x->key[i - 1];
                i--;
            }
            x->key[i] = k;
        } else {
            while (i - 1 < x->getN() && k < x->key[i - 1]) {
                i--;
            }
            if (x->child[i]->isFull()) {
                SplitChild(x, i);
                if (k > x->key[i]) {
                    i++;
                }
            }
            InsertNonFull(x->child[i].get(), k);
        }

    }

public:
    BTree() {
        root = std::make_unique<Node>();
    }

    [[nodiscard]] std::pair<const Node*, std::size_t> Search(const T& k) const {
        return Search(root.get(), k);
    }

    void Insert(const T& k) {
        if (root->isFull()) {
            auto s = std::make_unique<Node>();
            s->leaf = false;
            s->setN(0);
            s->child[0] = std::move(root);
            root = std::move(s);
            SplitChild(root.get(), 0);
            InsertNonFull(root.get(), k);
        } else {
            InsertNonFull(root.get(), k);
        }
    }

    friend std::ostream& operator<< (std::ostream& os, const BTree<T, t>& bt) {
        return os << *(bt.root) << '\n';
    }

};

int main() {
    BTree<int, 2> tree;
    tree.Insert(1);
    tree.Insert(2);
    tree.Insert(3);
    tree.Insert(4);
    tree.Insert(5);
    tree.Insert(6);
    tree.Insert(7);
    tree.Insert(8);
    tree.Insert(9);
    tree.Insert(10);

    std::cout << tree;

}