[英]c++: overloading + operator for a sparse matrix
void add(sparseMatrix<T> &b, sparseMatrix<T> &c); // c is output
sparseMatrix<T> operator+(sparseMatrix<T> &b);
I'm creating a sparse matrix which is made up of an arrayList of singly linked lists of matrix terms (matrix terms contain the row, column, and value). 我正在创建一个稀疏矩阵,它由矩阵项(矩阵项包含行,列和值)的单链接列表的arrayList组成。 I'm having trouble overloading the + operator.
我在重载+运算符时遇到问题。 I have an add method which works fine, but when I try to use it to overload the + operator I get the following errors:
我有一个可以正常工作的add方法,但是当我尝试使用它重载+运算符时,出现以下错误:
sparseMatrix.cpp: In function ‘int main()’:
sparseMatrix.cpp:268: error: no match for ‘operator=’ in ‘c = sparseMatrix<T>::operator+(sparseMatrix<T>&) [with T = int](((sparseMatrix<int>&)(& b)))’
sparseMatrix.cpp:174: note: candidates are: sparseMatrix<T>& sparseMatrix<T>::operator=(sparseMatrix<T>&) [with T = int]
make: *** [sparseMatrix] Error 1
Here is my implementation for the overloaded + operator: 这是我对重载+运算符的实现:
sparseMatrix<T> sparseMatrix<T>::operator+(sparseMatrix<T> &b)
{
sparseMatrix<T> c;
add(b, c);
return c;
}
The line in main that gives the error is c = a + b (a, b, c are all sparse matrices). main中给出错误的行是c = a + b(a,b,c均为稀疏矩阵)。 Note that if I do a.add(b,c) everything works fine.
请注意,如果我执行a.add(b,c),则一切正常。 I have also overloaded the = operator which works when I do a = b etc. but it seems to be complaining about it in the error message I posted.
我还重载了=运算符,该运算符在执行a = b等操作时有效,但似乎在我发布的错误消息中对此有所抱怨。 I'm really not sure what the problem is.
我真的不确定是什么问题。 Any ideas?
有任何想法吗?
note: candidates are: sparseMatrix& sparseMatrix::operator=(sparseMatrix&)
注意:候选者为:sparseMatrix&sparseMatrix :: operator =(sparseMatrix&)
Your operator=
should take a const reference. 您的
operator=
应该使用const引用。
If the reference isn't const, it can't be bound to a temporary, so the assignment operator can't be used for the temporary created by a + b
. 如果引用不是const,则不能将其绑定到临时对象,因此赋值运算符不能用于
a + b
创建的临时对象。
(The same is true for operator+
, here also the argument should be const sparseMatrix<T> &
. Additionally this method should be declared as const, since it doesn't modify the object it is called on.) (
operator+
也是如此,这里的参数也应为const sparseMatrix<T> &
。此外,此方法应声明为const,因为它不会修改调用的对象。)
sth: has correctly diagnosed the problem: 某事:已正确诊断问题:
But I would make your operators more standard. 但是我会让您的操作员更加标准。
class sparseMatrix
{
sparseMatrix(sparseMatrix const& copy);
sparseMatrix& operator=(sparseMatrix const& copy);
sparseMatrix& add(sparseMatrix const& value) // Add value to the current matrix
{
// Do Work.
return *this;
}
// Re-use add to implement the += operator.
sparseMatrix& operator+=(sparseMatrix const& rhs)
{
return add(rhs);
}
// Two things here:
//
// Implement the operator + in terms of the operator +=
//
// This basically means that you make a copy of one parameter then add the other
// value two it. Because + is symmetric it does not matter which you copy and which
// you add to the copy.
//
// So we pass the parameter by value this will provide an implicit copy
// generated by the compiler. This will also help the compiler with NRVO
// Then we just add (*this) to the copy of the rhs.
sparseMatrix operator+(sparseMatrix rhs)
{
return rhs += *this;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.