具有友元函数和运算符重载的 C++ 模板
C++ templates with friend function and operator overloading
我一直在将 C++ 模板添加到现有的运算符重载程序中,现在我遇到了这些错误。
我无法纠正这些错误,任何人都可以帮助我......
这是代码...
我可以在简单的重载函数上做模板,但在友元函数上有问题..
*****************注意:(更新)
整改后的代码...
/演示运算符过载的程序/
#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;
}
三个友元函数都出错,错误是
Now my friend function is not able to access the private member...<<<
Please tell me what I'm doing wrong...
如果您在 模板 class 中声明友元函数,您必须在模板 class 定义中提供定义,或者在模板外重新声明它模板 class.
在模板中将其声明为友元 class 不会在封闭范围内声明该函数。
我一直在将 C++ 模板添加到现有的运算符重载程序中,现在我遇到了这些错误。 我无法纠正这些错误,任何人都可以帮助我...... 这是代码... 我可以在简单的重载函数上做模板,但在友元函数上有问题.. *****************注意:(更新) 整改后的代码... /演示运算符过载的程序/ #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;
}
三个友元函数都出错,错误是
Now my friend function is not able to access the private member...<<< Please tell me what I'm doing wrong...
如果您在 模板 class 中声明友元函数,您必须在模板 class 定义中提供定义,或者在模板外重新声明它模板 class.
在模板中将其声明为友元 class 不会在封闭范围内声明该函数。