i'm trying to write a matrix class for c++, but i'm a bit rusty.
I have the following operators defined in my main program file but i'd like to include them in my header and implementation file instead. my syntax is a bit rusty but I think it should look something like this:
template%26lt;class itemType%26gt;
apmatrix%26lt;itemType%26gt; operator+(const apmatrix%26lt;itemType%26gt;%26amp; a, const apmatrix%26lt;itemType%26gt;%26amp; b)
{
//declare a matrix temp of type T to store the result and return this matrix
apmatrix%26lt;int%26gt; temp;
temp.resize(a.numrows(),b.numcols());
for(int i = 0; i %26lt; a.numrows(); i++)
for(int j = 0; j %26lt; a.numcols(); j++)
temp[i][j] = a[i][j] + b[i][j];
return temp;
}
I need to know how to change this to make it work, and what to put in my header.
the apmatrix class already has the operator = defined, but when i tried copying the similar format for my + operator in my header file i got this error:
C++ syntax help?
the plus operator is a binary not a tertiary operator - so it only takes one argument. in other words it's an operation between the current class and another parameter, so it wouldn't work with 3 things to operate on (this, a and b). Try this instead-
template%26lt;class T%26gt;
class m {
m%26lt;T%26gt; operator+(const m%26lt;T%26gt;%26amp; b) const
{
// use "(*this)" instead of "a"
// the function is const because it doesn't alter "this"
}
};
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment