简体   繁体   English

C ++模板和朋友声明

[英]C++ templates and friend declaration

Can someone tell me whats wrong with my code. 谁能告诉我我的代码有什么问题。 I'm guessing that I didn't overload << correctly, but I'm not sure how to fix it. 我猜我没有正确地重载<< ,但是我不确定如何解决它。

The below code implements a simple Stack container. 下面的代码实现了一个简单的Stack容器。 It fails at cout << si; 它在cout << si;处失败cout << si;

update: Made suggested changes, still not compiling. 更新:提出了建议的更改,但仍未编译。

update2: Got it! update2:知道了! Thanks! 谢谢!

#include <iostream>
using namespace std;

template <typename T = int, int N = 10>
struct Stack
{
    T elems[N];
    unsigned int size;

    Stack()
    {
        size=0;
    }

    void push(T e)
    {
        elems[size]=e;
        size++;
    }

    T pop()
    {
        size--;
        return elems[size];
    }

            template <typename T, int N>
    friend ostream& operator << (ostream& os, const Stack<T, N> &stack);
};

template <typename T, int N>
ostream& operator << (ostream& os, const Stack<T, N> &stack)
{
    for (unsigned int i=0; i<N; i++)
    {
        os << stack.elems[i];
    }

    return os;
}


int main()
{   

    Stack<> si;
    si.push(3);
    cout << si;

}

Should be 应该

ostream& operator << (ostream& os, const Stack<T,N> &stack);
//                                              ^^ -- note this

in both definition and declaration. 在定义和声明中。

You need to fully specify all template arguments for your stack here: 您需要在此处为​​堆栈完全指定所有模板参数:

template <typename T, int N>
ostream& operator<< (ostream& os, const Stack<T, N> &stack);

other wise the compiler can't deduce the proper N for your overloaded streaming operator. 否则,编译器无法为您的重载流运算符推断出适当的N

template <typename T, int N>
ostream& operator << (ostream& os, const Stack<T> &stack)

The problem with this template is that the parameter N cannot be inferred from either of the function arguments because you are using the default template argument for the Stack argument. 该模板的问题在于,由于您正在使用默认模板参数作为Stack参数,因此无法从两个函数参数中的任何一个推断参数N

Looking at your implementation, you almost certainly didn't intend this as you use N as the loop bound whereas Stack<T> has 10 elements. 查看您的实现,几乎可以肯定没有打算这样做,因为您使用N作为循环绑定,而Stack<T>有10个元素。 You probably meant to write: 您可能打算写:

template <typename T, int N>
ostream& operator << (ostream& os, const Stack<T, N> &stack)

Also, your friend declaration needs to match the template, at the moment the friend declaration is declaring a non-template friend overload. 同样,您的朋友声明需要匹配模板,而朋友声明正在声明非模板朋友重载。

This would declare an appropriate friend template. 这将声明一个适当的朋友模板。

template< typename S, int M >
friend ostream& operator << (ostream& os, const Stack<S, M> &stack);

You already got your answers, but may I suggest turning: 您已经得到了答案,但是我建议您转向:

void push(T e) 

into: 变成:

void push(const T& e) 

for performance wise, since you have no idea what T will be, and passing it on the stack isnt a good idea. 对于性能而言,由于您不知道T将是什么,因此将其传递到堆栈上并不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM