Home  >  Q&A  >  body text

c++模板类作为友元类出现的问题

#include <iostream>

using namespace std;

template <class T>
class SimNode
{
    friend SimSpace<T>;
private:
    T data;
    int link;
};

template <class T>
class SimSpace
{
private:
    SimNode<T>* space;
};

int main()
{
    system("pause");
    return 0;
}

报错之后问题出现在

friend SimSpace<T>;

请问为什么?

PHP中文网PHP中文网2764 days ago526

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:01:50

    There is something wrong with your statement, it should be

    class SimNode
    {
        
    private:
        template <class T>      //声明不要漏了template <class T>,这是C++很蛋疼的一个地方
        friend class SimSpace;  
        T data;
        int link;
    };

    reply
    0
  • Cancelreply