operator* is ambiguous
Apr 12, 2010 at 11:21pm UTC
Hey i'm trying to compile this program but it says that operator* is ambiguous. It worked until I added the second overload of that function and I called the operation. I've been looking for something over the forum but there's nothing helping me, may someone get a look?
Thanks, Enrico
vmatrix.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// ENRICO MAIORINO
#ifndef _VMATRIX_H_
#define _VMATRIX_H_
#include <vector>
using namespace std;
class VMatrix
{
public :
VMatrix(int rows, int cols);
VMatrix(int ord);
void init(int rows, int cols);
int rows()const ;
int cols()const ;
vector<double >& operator [] (int r);
vector<double > operator [] (int r)const ;
VMatrix& operator =(double k);
VMatrix& operator =(const VMatrix &r);
VMatrix operator +(const VMatrix &add);
VMatrix operator -(const VMatrix &sub);
VMatrix operator *(double k)const ;
VMatrix operator *(const VMatrix &r);
VMatrix operator /(double k)const ;
VMatrix& operator +=(const VMatrix &add);
VMatrix& operator -=(const VMatrix &sub);
VMatrix& operator *=(const VMatrix &r);
bool operator ==(const VMatrix &r);
VMatrix extract_r(int n);
VMatrix& fill();
void print();
private :
vector<vector<double > > m;
};
#endif
vmatrix.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#include <iostream>
#include <vector>
#include <vmatrix.h>
using namespace std;
VMatrix::VMatrix(int rows, int cols)
{
init(rows,cols);
}
VMatrix::VMatrix(int ord)
{
init(ord,ord);
}
void VMatrix::init(int rows, int cols)
{
m.clear();
for (int i = 0; i < rows; i++)
{
vector<double > r(cols);
m.push_back(r);
}
//cout << "Ho " << m.size() << " righe e " << m[0].size() << " colonne" <<endl;
}
int VMatrix::rows()const
{
return m.size();
}
int VMatrix::cols()const
{
return m[0].size();
}
vector<double >& VMatrix::operator [] (int r)
{
if (r < rows()) return m[r];
cout << "Il numero di riga richiesto (" <<r <<") non e' valido." <<endl;
return m[0];
}
vector<double > VMatrix::operator [] (int r)const
{
if (r < rows()) return m[r];
cout << "Il numero di riga richiesto (" <<r <<") non e' valido." <<endl;
return m[0];
}
VMatrix& VMatrix::operator = (double k)
{
for (int i=0;i<rows();i++)
{
for (int j=0;j<cols();j++)
{
m[i][j] = k;
}
}
return *this ;
}
VMatrix& VMatrix::operator =(const VMatrix &r)
{
m = r.m;
return *this ;
}
VMatrix VMatrix::operator + (const VMatrix &add)
{
if (rows() == add.rows() && cols() == add.cols())
{
VMatrix r(rows(),cols());
for (int i=0;i<rows();i++)
{
for (int j=0;j<cols();j++)
{
r[i][j] = m[i][j] + add[i][j];
}
}
return r;
}
cout << "Operazione di somma non valida." <<endl;
return VMatrix(0);
}
VMatrix VMatrix::operator - (const VMatrix &sub)
{
if (rows() == sub.rows() && cols() == sub.cols())
return (*this ) + sub * (-1.);
cout << "Operazione di sottrazione non valida." <<endl;
return VMatrix(0);
}
VMatrix VMatrix::operator * (double k)const
{
VMatrix r(rows(),cols());
for (int i=0;i<rows();i++)
{
for (int j=0;j<cols();j++)
{
r[i][j] = m[i][j] * k;
}
}
return r;
}
VMatrix VMatrix::operator * (const VMatrix &r)
{
if (cols() == r.rows())
{
VMatrix p(rows(),r.cols());
p = 0;
for (int i=0;i<p.rows();i++)
{
for (int j=0;j<p.cols();j++)
{
for (int k=0;k<cols();k++)
{
p[i][j] += m[i][k] * r[k][j];
}
}
}
return p;
}
cout << "Operazione di prodotto non valido." <<endl;
return VMatrix(0);
}
VMatrix VMatrix::operator / (double k)const
{
return *this * (1/k);
}
VMatrix& VMatrix::operator +=(const VMatrix &add)
{
*this = *this + add;
return *this ;
}
VMatrix& VMatrix::operator -=(const VMatrix &sub)
{
*this = *this - sub;
return *this ;
}
VMatrix& VMatrix::operator *=(const VMatrix &r)
{
*this = *this * r;
return *this ;
}
bool VMatrix::operator ==(const VMatrix &r)
{
return (m == r.m);
}
VMatrix VMatrix::extract_r(int n)
{
if (n < rows())
{
VMatrix r(cols(),1);
for (int i=0;i<r.cols();i++)
{
r.m[i][0] = m[i][0];
}
return r;
}
cout << "Il numero di riga richiesto (" << n << ") non e' valido." ;
return VMatrix(0);
}
VMatrix& VMatrix::fill()
{
int k=0;
//cout <<cols()<<" "<<rows();
for (int i=0;i<rows();i++)
{
for (int j=0;j<cols();j++)
{
m[i][j] = k;
k++;
}
}
return *this ;
}
void VMatrix::print()
{
for (int i=0;i<rows();i++)
{
for (int j=0;j<cols();j++)
{
cout<<m[i][j]<<"\t" ;
}
cout<<endl;
}
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <vector>
#include <vmatrix.h>
using namespace std;
int main()
{
VMatrix m1(2);
VMatrix m2(2);
VMatrix m3(2);
m1.fill().print();
m2.fill().print();
m2 = m1 * 3.;
return 0;
}
Apr 12, 2010 at 11:37pm UTC
i resolved the problem, I just had to call the constructor
with the "explicit" keyword before, to avoid implicit conversion of double->int->VMatrix(int)
thanks anyways :D
Topic archived. No new replies allowed.