Hola a todos, estoy haciendo una clase vetor y quiero sobrecargar la suma, no entiendo cual es el problema con lo que escribi, cuando compilo la sobrecarga no da problema pero cuando escribo C=A+B, donde A, B son vectores me aparece un mensaje que dice "No matching function for call"vetor::vetor(vetor)'
Esta en la sobrecarga
vetor vetor::operator+(const vetor& a){
vetor soma(dim);
for (int i=0; i<dim; i++) {
soma.v[i]=v[i]+a.v[i];}
return soma;
}
y cuando lo escribo de la siguiente manera para tener en cuenta la dimensión del vector, aparece el mismo error anterior mas otros dos en la declaración de la sobrecarga, asi
vetor vetor::operator+(const vetor& a){
if (dim != a.dim)
return 0; // aqui me aparecen dos errores, el que comenté anteriormente y otro que dice initializing temporary from result of 'vetor::vetor(int)
vetor soma(dim);
for (int i=0; i<dim; i++) {
soma.v[i]=v[i]+a.v[i];}
return soma;
}
Muchas gracias por cualquier ayuda que me puedan brindar.
#include <iostream>
using namespace std;
//%%%%%%%%%%%%%%%%%%%%%%%%%%% ** class ** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class vetor
{
double *v;
int dim;
public:
vetor (int n);
vetor(vetor& W);
~vetor();
void set(int i, double x);
double get(int i);
int get_dim();
vetor operator+(const vetor& a);
};
//%%%%%%%%%%%%%%%%%%%%%%% ** implementación ** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vetor::vetor (int n){
dim=n;
v=new double [n];}
vetor::vetor(vetor& C){
dim=C.get_dim();
v=new double[dim];
for (int j=0; j<dim; j++) {
v[j]=C.get(j);
}}
vetor::~vetor(){
delete[]v;}
void vetor::set(int i, double x){
if ((i>=0)&&(i<dim)) {
v[i]=x;
}}
double vetor::get(int i){
if((i>=0)&&(i<dim))
return (v[i]);
else
return 0;
}
int vetor::get_dim(){
return (dim);}
vetor vetor::operator+(const vetor& a){
vetor soma(dim);
for (int i=0; i<dim; i++) {
soma.v[i]=v[i]+a.v[i];}
return soma;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%% ** main ** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main (int argc, char * const argv[]) {
int i,n;
cout << "entre com a dimensao do vetor = ";
cin>> n;
vetor A(n);
vetor B(n);
vetor C(n);
cout << "Entre com os elementos do vetor "<< endl;
for (i=0; i<n; i++) {
double x;
cout << "v["<<i<<"]=";
cin>>x;
A.set(i,x);
cout << "W["<<i<<"]=";
cin>>x;
B.set(i,x);
}
for (i=0; i<n; i++) {
cout << "v["<<i<<"]="<<A.get(i)<<endl;
}
C = A+B; //aqui es el problema
/*for (i=0; i<n; i++) {
cout << "v["<<i<<"]="<<B.get(i)<<endl;
}
for (i=0; i<n; i++) {
cout << "v["<<i<<"]="<<C.get(i)<<endl;
}*/
return 0;
}