运算符重载并导致友元函数错误
我正在做一项向我介绍运算符重载的作业。我必须重载一些二元运算符作为成员函数和友元函数。我的重载“+”运算符的成员函数工作正常,但我的重载“-”运算符的友元函数似乎无法找到该成员函数能够使用的数据。
类定义:
class matrix
{
friend ostream& operator << (ostream&, const matrix&);
friend bool operator == (const matrix &, const matrix &);
friend matrix operator - (const matrix &, const matrix &);
private:
int size;
int range;
int array[10][10];
public:
matrix(int);
matrix(int, int);
bool operator != (const matrix &) const;
matrix operator + (const matrix &) const;
const matrix & operator = (const matrix &);
};
“+”重载:
matrix matrix::operator + (const matrix & a) const
{
matrix temp(size,range);
for (int i = 0; i < a.size; i++)
for (int j = 0; j < a.size; j++)
temp.array[i][j] = a.array[i][j] + array[i][j];
return temp;
}
“-”重载:
matrix operator - (const matrix & a, const matrix & b)
{
matrix temp(size, range);
for (int i = 0; i < a.size; i++)
for (int j = 0; j < a.size; j++)
temp.array[i][j] = a.array[i][j] - array[i][j];
return temp;
}
我在友元函数中遇到的错误是大小、范围和数组均未声明。我很困惑,因为我认为成员函数和友元函数都具有对类中数据的同等访问权限,并且我基本上在这两个函数中执行相同的操作。有谁知道我的问题可能是什么?
I am working on an assignment that introduces me to operator overloading. I have to overload some binary operators as member functions and also as friend functions. My member function that overloads the "+" operator works fine but my friend function that overloads the "-" operator seems to have trouble finding data that the member function is able to use.
class def:
class matrix
{
friend ostream& operator << (ostream&, const matrix&);
friend bool operator == (const matrix &, const matrix &);
friend matrix operator - (const matrix &, const matrix &);
private:
int size;
int range;
int array[10][10];
public:
matrix(int);
matrix(int, int);
bool operator != (const matrix &) const;
matrix operator + (const matrix &) const;
const matrix & operator = (const matrix &);
};
"+" overload:
matrix matrix::operator + (const matrix & a) const
{
matrix temp(size,range);
for (int i = 0; i < a.size; i++)
for (int j = 0; j < a.size; j++)
temp.array[i][j] = a.array[i][j] + array[i][j];
return temp;
}
"-" overload:
matrix operator - (const matrix & a, const matrix & b)
{
matrix temp(size, range);
for (int i = 0; i < a.size; i++)
for (int j = 0; j < a.size; j++)
temp.array[i][j] = a.array[i][j] - array[i][j];
return temp;
}
The error I am getting in the friend function is that size, range, and array are all undeclared. I am confused because I thought member and friend functions both had equal access to data in a class and I basically doing the same thing in both functions. Does anyone know what my issue may be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
友元运算符不是该类的一部分。因此,它不知道
size
和range
和array
。您必须使用对象a
和b
。它应该是这样的:The friend operator is not part of the class. Thus, it does not know
size
andrange
andarray
. You must use the objectsa
andb
. It should be something like this:虽然您的友元函数将能够访问对象的私有数据,但这并不意味着这些属性在该函数的范围内。我的意思是,它不会像您期望的那样充当成员函数。您需要提供您传递的对象之一的大小。
Though your friend function would be able to access the private data of the Objects, but this doesn't imply that the attributes are in the scope of that function. I mean, it wont act like a member function as you are expecting it to. You would need to provide the size from one of the objects that you are passing.