如何使一个模板类成为另一个模板类的友元

huangapple go评论80阅读模式
英文:

How to make a templated class a friend of another templated class

问题

以下是您要翻译的内容:

出于教育目的,我正在编写一个基于单链表的模板化堆栈类。我已经为节点编写了一个类:

```cpp
template <typename T>
class StackNode {
    private:
        T data;
        StackNode<T> *next;
        // TODO: 让Stack成为友元
};

然后我开始编写堆栈本身:

template <typename T>
class Stack {
    private:
        StackNode<T> *head;
        unsigned long long int size;
    public:
        Stack(): head(nullptr), size(0) {}
        void push(const T& element) {
            StackNode<T> *new_element = new StackNode<T>;
            new_element -> data = element;
            new_element -> next = head;
            head = new_element;
            ++size;
        }
};

显然,这两行

            new_element -> data = element;
            new_element -> next = head;

给我带来了麻烦,因为我试图访问私有成员。我想让Stack成为StackNode的友元。我该如何做?

当我在StackNode的TODO行中写入friend class Stack<T>时,我的CLion IDE用红色波浪线下划线标出了Stack,并表示我将Stack重新定义为不同类型的符号。我不知道该如何解决这个问题。

更新:
我的main函数如下:

int main() {
    Stack<long long int> s;
    s.push(12);
    return 0;
}

感谢任何帮助。


<details>
<summary>英文:</summary>

For educational purposes I&#39;m writing a templated Stack class based on a singly linked list. I have written a class for a node:

template <typename T>
class StackNode {
private:
T data;
StackNode<T> *next;
// TODO: make Stack a friend
};

and I started writing the stack itself:

template <typename T>
class Stack {
private:
StackNode<T> *head;
unsigned long long int size;
public:
Stack(): head(nullptr), size(0) {}
void push(const T& element) {
StackNode<T> *new_element = new StackNode<T>;
new_element -> data = element;
new_element -> next = head;
head = new_element;
++size;
}
};

Obviously, these two lines
        new_element -&gt; data = element;
        new_element -&gt; next = head;
are giving me trouble, because I&#39;m trying to get access to private members. I want to make ```Stack``` a friend of ```StackNode```. How can I do that?

When I write ```friend class Stack&lt;T&gt;``` in my TODO line in the ```StackNode```, my CLion IDE underlines ```Stack``` with a red squiggly line and it says that I have redefinition of ```Stack``` as a different kind of symbol. I don&#39;t know how to fix that.

UPD:
my ```main``` function looks like this:

int main() {
Stack<long long int> s;
s.push(12);
return 0;
}


I appreciate any help.


</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

"Well, your issue is still not reproducible / complete.
If you want to such an issue to be solved, try an online compiler like wandbox.
 An example is complete, if you can copy paste it into an online compiler and it leads to the error, that you want to be solved.

We can only guess what your issue is. Maybe you missed the forward decl. This way a friend declaration is done with the same class dependency structure as in your example:

#include <iostream>;

template<class T>
class Foo;

template<class T>
class Bar
{
public:
    Bar(T t) : v(t) {}
        
private:
    T v;
    friend class Foo<T>;
};

template<class T>
class Foo
{
public:
    void Fun(Bar<T> bar)
    {
        std::cout << bar.v;
    }
};

int main()
{
    Bar<int> bar(99);
    Foo<int> foo;
    foo.Fun(bar);
}"


<details>
<summary>英文:</summary>

Well, your issue is still not reproducible / complete.
If you want to such an issue to be solved, try an online compiler like wandbox.
 An example is complete, if you can copy paste it into an online compiler and it leads to the error, that you want to be solved.

We can only guess what your issue is. Maybe you missed the forward decl. This way a friend declaration is done with the same class dependency structure as in your example:

    #include &lt;iostream&gt;
    
    template&lt;class T&gt;
    class Foo;
    
    template&lt;class T&gt;
    class Bar
    {
    public:
        Bar(T t) : v(t) {}
        
    private:
        T v;
        friend class Foo&lt;T&gt;;
    };
    
    template&lt;class T&gt;
    class Foo
    {
    public:
        void Fun(Bar&lt;T&gt; bar)
        {
            std::cout &lt;&lt; bar.v;
        }
    };
    
    int main()
    {
        Bar&lt;int&gt; bar(99);
        Foo&lt;int&gt; foo;
        foo.Fun(bar);
    }

</details>



huangapple
  • 本文由 发表于 2020年1月3日 19:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定