I will try to explain the best I can the problem:
I have a class Polinomio (Polynomial in spanish). that has a vector of the class Monomio (Monomial).
In Polinomio I have this method:
void Polinomio::rellenarRandom() {
Monomio mon;
vector<Monomio> vec (getGrado() + 1, mon);
for (int i = 0; i <= getGrado(); i++) {
int random = MIN + (rand() % (int)(MAX - MIN + 1));
Monomio mon (random, i);
vec[i] = mon;
}
cout << vec[0] << endl;
cout << vec[1] << endl;
cout << vec[2] << endl;
cout << vec[3] << endl;
cout << vec[4] << endl;
setMonomio(vec);
}
It fills with random Monomials the vector vec, then it sets the vetor "Monomio" (bad named, I know) to this random vector.
The cout's make this output (it varies, but an example):
- -11x^0
- -69x^1
- 28x^2
- -78x^3
- -53x^4
The setter is pretty simple:
void Polinomio::setMonomio(vector<Monomio> vec) {
this->monomios = vec;
}
And in the main I have:
Polinomio pol (5); // Creates a Polinomial with grade 5
pol.rellenarRandom();
cout << "pol: " << pol << endl;
That cout makes this output:
pol: 3473744x^0 + -69x^1 + 28x^2 + -78x^3 + -53x^4 + -9x^5 +
I tested many times, to try to detect the mistake, but it appears to follow no pattern. The first number is different every time, but never the one it should be.
It's not a problem of the overload of << on the Polinomo class, since I also tested another access to that element, and it shows it "correctly" (the same, even if it's not what it should).
The problem must be on the setter, but I ust can't figure out what the problem is.
Thanks for your help!
EDIT: (modified the setter name that was wrong to setMonomio
I added one more test, and it's driving me CRAZY. This is what I did:
I modified this cout before the set, to see the member that gives me problems:
cout << "VEC :"; cout << vec[0] << endl;
setMonomios(vec);
In setMonomios, I modified setMonomios:
void Polinomio::setMonomios(vector vec) { cout << "set: " << vec[0] << endl; this->monomios = vec; cout << "postset: " << this->monomios[0] << endl; }
result:
- VEC :0x^0
- set: 3670352x^0
- postset: 3670352x^0
So, the valor get's modified somehow when I call the setter?
ANSWERS (EDIT's you have asked for):
Tony Jiang:
Did that, the result was (only with the first element, that gives problems): * VEC :0x^0 * set: 3670352x^0 * postset: 3670352x^0
Wolfgang.Finkbeiner:
The << operator overload:
inline ostream& operator<< (ostream& o, const Polinomio &a) {
auto v = a.getMonomios();
for (vector<Monomio>::iterator it = v.begin(); it != v.end(); ++it)
o << *it << " + ";
return o;
}
DOUGLAS O. MOEN
getGrado does always return the same value. The reason I don't use vector.size () is because in some other functions I use it before initializing the vector, or while modifying it, so it can have unexpected results if I do. Anyway, it's tested, it doesn't give any problem.
PaulMcKenzie
I don't know what you mean exactly by the copy semantics, but anyway, I'll post it:
#include "Monomio.h"
Monomio::Monomio() {
coeficiente = 0;
exponente = 0;
}
Monomio::Monomio(int coef) {
coeficiente = coef;
exponente = 0;
}
Monomio::Monomio(int coef, int exp) {
coeficiente = coef;
exponente = exp;
}
Monomio::Monomio(const Monomio& orig) {
}
Monomio::~Monomio() {
}
//Getters & Setters:
int Monomio::getCoeficiente() const {
return coeficiente;
}
int Monomio::getExponente() const {
return exponente;
}
void Monomio::setCoeficiente(int c) {
this->coeficiente = c;
}
void Monomio::setExponente(int e) {
this->exponente = e;
}
R Sahu
The MCVE (maybe not exactly Minimal, but it's a small project):
Apart from the class Monomio (posted above) to reproduce you might need:
The class Polinomio:
#include "Polinomio.h"
Polinomio::Polinomio() {
setGrado(0);
setMonomios(vector<Monomio> (0));
srand (time(NULL));
}
Polinomio::Polinomio(const Polinomio& orig) {
setGrado(orig.getGrado());
setMonomios(orig.getMonomios());
srand (time(NULL));
}
Polinomio::Polinomio(int grado) {
setGrado(grado);
cout << "grado:" << getGrado() << endl;
Monomio mon;
setMonomios(vector<Monomio> (getGrado() + 1, mon));
srand (time(NULL));
}
Polinomio::~Polinomio() {
}
int Polinomio::getGrado() const {
return this->grado;
}
vector<Monomio> Polinomio::getMonomios() const {
return this->monomios;
}
void Polinomio::rellenarRandom() {
Monomio mon;
vector<Monomio> vec (getGrado() + 1, mon);
for (int i = 0; i <= getGrado(); i++) {
// int random = MIN + (rand() % (int)(MAX - MIN + 1));
int random = 1 + i;
Monomio mon (random, i);
vec[i] = mon;
}
cout << "VEC :";
cout << vec[0] << endl;
setMonomios(vec);
auto v = monomios;
cout << "V: ";
cout << v[0] << endl;
}
void Polinomio::rellenarRandom(int grado) {
}
void Polinomio::setGrado(int grado) {
this->grado = grado;
}
void Polinomio::setMonomios(vector<Monomio> vec) {
cout << "set: " << vec[0] << endl;
this->monomios = vec;
cout << "postset: " << this->monomios[0] << endl;
}
And the Header files (for the operators mainly, but in case you see anything weird please tell me):
#ifndef POLINOMIO_H
#define POLINOMIO_H
#include "Monomio.h"
class Polinomio {
int grado;
public: vector<Monomio> monomios;
public:
Polinomio();
Polinomio (int grado);
Polinomio(const Polinomio& orig);
virtual ~Polinomio();
int getGrado () const;
void setGrado (int grado);
void rellenarRandom ();
void rellenarRandom (int grado);
vector<Monomio> getMonomios () const;
void setMonomios (vector<Monomio> vec);
private:
const int MAX = 100;
const int MIN = -100;
};
//Operators:
inline ostream& operator<< (ostream& o, const Polinomio &a) {
auto v = a.getMonomios();
for (vector<Monomio>::iterator it = v.begin(); it != v.end(); ++it)
o << *it << " + ";
return o;
}
inline Polinomio operator+(const Polinomio &a, const Polinomio &b) {
}
inline Polinomio operator-(const Polinomio &a, const Polinomio &b) {
}
inline Polinomio operator*(const Polinomio &a, const Polinomio &b) {
}
#endif /* POLINOMIO_H */
#ifndef MONOMIO_H
#define MONOMIO_H
#include <cstdlib>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
class Monomio {
int coeficiente;
int exponente;
public:
Monomio();
Monomio(int coef);
Monomio(int coef, int exp);
Monomio(const Monomio& orig);
int getCoeficiente() const;
int getExponente() const;
void setExponente(int e);
void setCoeficiente(int c);
virtual ~Monomio();
private:
};
//Operators:
inline ostream& operator<< (ostream& o, const Monomio &a) {
o << a.getCoeficiente() << "x^" << a.getExponente();
return o;
}
inline Monomio operator+(const Monomio &a, const Monomio &b) {
Monomio res;
if (a.getExponente() != b.getExponente())
throw "Suma de monomios con exponente diferente!";
else {
res = Monomio (a.getCoeficiente() + b.getCoeficiente(), a.getExponente());
}
return res;
}
inline Monomio operator-(const Monomio &a, const Monomio &b) {
Monomio res;
if (a.getExponente() != b.getExponente())
throw "Suma de monomios con exponente diferente!";
else {
res = Monomio (a.getCoeficiente() - b.getCoeficiente(), a.getExponente());
}
return res;
}
inline Monomio operator*(const Monomio &a, const Monomio &b) {
Monomio res = Monomio (a.getCoeficiente() * b.getCoeficiente(), a.getExponente() + b.getExponente());
return res;
}
#endif /* MONOMIO_H */
Plus: the Main file I use (I also did a few tests for Monomio:
#include "Polinomio.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
Monomio a (2, 2);
Monomio b (3, 5);
Monomio c (3, 2);
cout << a << " + " << c << " = " << a + c << endl;
cout << a << " - " << c << " = " << a - c << endl;
cout << a << " * " << b << " = " << a * b << endl;
Polinomio pol (5);
pol.rellenarRandom();
cout << "pol: " << pol << endl;
cout << "primero: " << pol.getMonomios().at(0);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire