startIndex]; 边[ch]=e; } 结构边{ Node*next=新节点(); int startIndex=0; friend std::ostream&operator,c++,syntax,organization,C++,Syntax,Organization" /> startIndex]; 边[ch]=e; } 结构边{ Node*next=新节点(); int startIndex=0; friend std::ostream&operator,c++,syntax,organization,C++,Syntax,Organization" />

C++;-结构需要访问类的私有实例变量 我的C++有点生疏了,我想知道你能否帮助我。基本上,我有两个结构和一个类,结构需要访问类的私有实例变量 #ifndef TREE_H #define TREE_H #include <iostream> struct Node { ... inline void addEdge(Edge* e); }; inline void Node::addEdge(Edge* e) { char ch = inp[e->startIndex]; edges[ch] = e; } struct Edge { Node* next = new Node(); int startIndex = 0; friend std::ostream& operator<<(std::ostream& out, Edge e) { int index = inp.size() - e.startIndex + endIndex + 1; // Here the edge needs access to the private members of the Tree class such as string inp, and int endIndex // ... do things with index return out; } }; class Tree { public: Tree(); friend std::ostream& operator<<(std::ostream& out, Tree s); private: Node* root = nullptr; int endIndex = 0; std::string inp; void foo(); std::ostream& printTree(std::ostream& out, Node* curr, std::string append=""); }; #endif // TREE_H \ifndef TREE\u H #定义树 #包括 结构节点{ ... 内联无效添加(边*e); }; 内联void节点::addEdge(边*e){ char ch=inp[e->startIndex]; 边[ch]=e; } 结构边{ Node*next=新节点(); int startIndex=0; friend std::ostream&operator

C++;-结构需要访问类的私有实例变量 我的C++有点生疏了,我想知道你能否帮助我。基本上,我有两个结构和一个类,结构需要访问类的私有实例变量 #ifndef TREE_H #define TREE_H #include <iostream> struct Node { ... inline void addEdge(Edge* e); }; inline void Node::addEdge(Edge* e) { char ch = inp[e->startIndex]; edges[ch] = e; } struct Edge { Node* next = new Node(); int startIndex = 0; friend std::ostream& operator<<(std::ostream& out, Edge e) { int index = inp.size() - e.startIndex + endIndex + 1; // Here the edge needs access to the private members of the Tree class such as string inp, and int endIndex // ... do things with index return out; } }; class Tree { public: Tree(); friend std::ostream& operator<<(std::ostream& out, Tree s); private: Node* root = nullptr; int endIndex = 0; std::string inp; void foo(); std::ostream& printTree(std::ostream& out, Node* curr, std::string append=""); }; #endif // TREE_H \ifndef TREE\u H #定义树 #包括 结构节点{ ... 内联无效添加(边*e); }; 内联void节点::addEdge(边*e){ char ch=inp[e->startIndex]; 边[ch]=e; } 结构边{ Node*next=新节点(); int startIndex=0; friend std::ostream&operator,c++,syntax,organization,C++,Syntax,Organization,我建议使用访问者模式 下面是一个简短、不完整的示例,让您了解: class Tree { Node _Node; std::list<Node> _Children; public: ... // other stuff, like AddChild(), etc. void Traverse(Visitor visitor) { visitor.HandleNodeData(_Node.Data); trav

我建议使用访问者模式

下面是一个简短、不完整的示例,让您了解:

class Tree
{
    Node _Node;
    std::list<Node> _Children;
public:
    ... // other stuff, like AddChild(), etc.

    void Traverse(Visitor visitor)
    {
        visitor.HandleNodeData(_Node.Data);
        traverseChildren(visitor);
    }
};
类树
{
节点_节点;
性病:列出儿童;
公众:
…//其他内容,如AddChild()等。
无效遍历(访客)
{
visitor.HandleNodeData(_Node.Data);
儿童(访客);
}
};

我也会考虑将树类作为模板来实现。

对于任何人的未来引用,我现在用指针解决方案;似乎完成了任务。我唯一的疑问是树类中的EndoDeq是否需要在堆栈上(我想是的)。有人可以澄清吗?
#ifndef TREE_H
#define TREE_H

#include <iostream>

struct Node {
    ...
    inline void addEdge(Edge* e);
};

inline void Node::addEdge(Edge* e) {
    char ch = inp[e->startIndex];
    edges[ch] = e;
}

struct Edge {
    Edge(int start, int* endIndex) {        
        this->startIndex = start;
        this->endIndex = endIndex;
    }
    Node* next = new Node();
    int startIndex = 0;
    int* endIndex;
    friend std::ostream& operator<<(std::ostream& out, Edge e) {
        std::string value = inp.substr(e.startIndex,  *e.endIndex - e.startIndex + 1);
        out << "(---- " << value << "[" << e.startIndex << ", " << *e.endIndex << "] ---->)";
        return out;
    }
};


class Tree {
public:
    Tree();
    friend std::ostream& operator<<(std::ostream& out, Tree s);

private:
    Node* root = nullptr;
    int* endIndex = new int;   // am i correct in saying this needs to be on the stack? 
    std::string inp;
    void foo();
    std::ostream& printTree(std::ostream& out, Node* curr, std::string append="");

};
#endif // TREE_H
\ifndef TREE\u H
#定义树
#包括
结构节点{
...
内联无效添加(边*e);
};
内联void节点::addEdge(边*e){
char ch=inp[e->startIndex];
边[ch]=e;
}
结构边{
边(int开始,int*endIndex){
此->开始索引=开始;
此->endIndex=endIndex;
}
Node*next=新节点();
int startIndex=0;
int*endIndex;

friend std::ostream&Operator您似乎知道什么是
friend
,因为您将该声明用于
Operator请注意
friend
不是最佳解决方案,甚至无法帮助您实现所有目标。首先,
节点
边缘
类需要了解
类,这意味着你需要对声明和函数定义重新排序。然后
节点
边缘
类还需要知道它们所属的特定
实例。这将创建一个设计非常糟糕的依赖项和链接网络,这将很难理解和维护。你可能想重新创建取消你的设计,看看你是否能想出更好的方法来处理你需要做的事情。你的评论正是我所关心的。friend解决方案依赖于他们知道他们所属的树的实例。问题是,我实际上需要结构不仅仅是访问endIndex。如果树的endIndex发生变化,所有的边缘都会消失s需要使用这个新的endIndex而不需要我更新它们(一个O(1)操作)。你认为在创建边时传递一个指向endIndex的指针会是一个很好的解决方案吗?我想你可能误解了我的意思;我并不是在写一个树类本身(我只是将代码简化为现在的样子,所以在这里发布不会太长).实际上,我正在编写一个复杂的数据结构,它具有苛刻的复杂性要求,因此我需要以O(1)方式引用endIndex的边。你说“我需要以O(1)方式引用endIndex的边”是什么意思?
我认为访问者模式仍然是一种选择,因为您可以将私有数据传递给访问者。我肯定会仔细阅读访问者模式。我在O(1)中引用的意思是什么方式是这样的。边通过使用子字符串的索引位置来跟踪主输入字符串的子字符串。随着endIndex的更改,每个边存储的子字符串有效地更改,因为索引更改。但这是在O(1)中完成的因为实际上不必手动更改每个边存储的子字符串值。