简体   繁体   English

具有友元函数和运算符重载的C ++模板

[英]C++ templates with friend function and operator overloading

I have been adding c++ templates to an existing Operator overloading program, and now I'm getting these errors. 我一直在向现有的Operator重载程序添加c ++模板,现在我收到了这些错误。 I'm not able to rectify these errors, can anybody help me... Here's the code... I'm able to template on simple overloaded function but having problem on friend function.. *****************NOTE:(Updated) Code after Rectification... / PROGRAM TO DEMONSTRATE OPERATOR OVERLOADING / #include 我无法纠正这些错误,任何人都可以帮助我......这是代码......我能够在简单的重载函数上模板但在朋友函数上有问题.. ********* ********注意:(更新)整改后的代码... / 程序演示操作员超载 / #include

template <class T>
class OP 
{
    T x, y, z;

    public:

    void IN()
    {
        std::cout << "Enter three No's : ";
        std::cin >> x >> y >> z;
    }

    void OUT()
    {
        std::cout << std::endl << x << " " << y << " " << z;
    }
    void operator~();
    void operator+(int);                    //Can accept both one or two argument

    template<class TA>
    friend void operator++(OP<T>);             //Accept one arguments (Unary Operator);

    template<class TB>
    friend void operator-(OP<T>, int);     //Accept two arguments     (Binary Operator)

    template<class TC>
    friend void operator!=(OP&, char t);
};

template<class T>
void OP<T>::operator~() //Unary Member Operator
{
    std::cout << std::endl << " ~ (tilde)";
}

template<class T>
void OP<T>::operator+(int y)    //Binary Member Operator
{
    std::cout << std::endl << "Argument sent is " << y;
}

template<class T>
void operator-(OP<T> &a, int t) //Binary Friend Operator
{
    a.x= a.x-t;
    a.y= a.y-t;
    a.z= a.z-t;
}

template<class T>
void operator!=(OP<T> &q, char t)   //Binary Friend Operator
{
    std::cout << std::endl << "Char " << t; 
}

template<class T>
void operator++(OP<T> x)    //Unary Friend Operator
{
    std::cout << std::endl << "Friend Unary Operator";
}

int main()
{
    OP <int> n, m;
    n.IN();
    m.IN();
    m+1;
    n!='t';
    int a = 1;
    char r='r';
    ~n;         //Member Function (Unary)
    n-a;        //Member Function (Unary)
    operator-(m, a);    //Friend Function (Binary)
    operator++(m);  //Friend Function (Unary)
    n.OUT();
    m.OUT();
    std::cin.get();
    return 0;
}

The error is on all three friend function and the error is 错误是在所有三个朋友功能上,错误是

Now my friend function is not able to access the private member...<<< Please tell me what I'm doing wrong... 现在我的朋友功能无法访问私人会员... <<<请告诉我我做错了什么...

If you declare a friend function within a template class, you must either provide the definition within the template class definition, or redeclare it outside the template class. 如果在模板类中声明了友元函数,则必须在模板类定义中提供定义,或者在模板类之外重新声明它。

Declaring it as a friend with in the template class does not declare that function in the enclosing scope. 在模板类中将其声明为朋友并不在封闭范围内声明该函数。

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

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