### Problem vector 3D

Helo everyone today i have a doubt about the code below , it's about the management of the vector in 3D , afteer show you the code then i have the specifics questions about it , and you'll tell me if you can help me , so the code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155`` ``````#include #include #include using namespace std; // Pour simplifier typedef string Forme ; typedef string Couleur ; class Brique { private: Forme forme ; Couleur couleur ; public: /***************************************************** Compléter le code à partir d'ici *******************************************************/ Brique (Forme a, Couleur b):forme(a),couleur(b){} Forme get_forme()const{return forme;} Couleur get_couleur() const{return couleur;} ostream& afficher(ostream& sortie) const { sortie<<"( "<>> contenu; public: Construction (Brique a); unsigned int get_contenu_size()const{return contenu.size();} unsigned int get_contenu_size(int i)const{return contenu[i].size();} unsigned int get_contenu_size(int i, int j)const{return contenu[i][j].size();} Brique get_contenu(int i=0, int j=0, int k=0)const{return contenu[i][j][k];} vector>> get_contenuVect()const{return contenu;} ostream& afficher(ostream& ) const; void operator +=(Construction const & a); }; ostream& operator <<(ostream& sortie , Construction const & tmp) { return tmp.afficher(sortie); } ostream& Construction::afficher(ostream& os) const { unsigned int i(0),j(0),k(0); if (contenu.empty()) return os; else { for(i =0; i > (1,vector (1, a))) {} /******************************************* * Ne rien modifier après cette ligne. *******************************************/ int main() { // Modèles de briques Brique toitD("obliqueD", "rouge"); Brique toitG("obliqueG", "rouge"); Brique toitM(" pleine ", "rouge"); Brique mur (" pleine ", "blanc"); Brique vide (" ", ""); /**unsigned int largeur(4); unsigned int profondeur(3); unsigned int hauteur(3); // sans le toit */ // on construit les murs Construction maison(mur+mur+mur); Construction maison2(mur); //cout<

Questions:

So let's say that i , j , k are the dimensions but for the moment i can't get the result of the + operator
If you see in the main i have this:

Construction maison(mur+mur+mur);
cout<<maison ;

if i compile and execute i obtain:

Couche 0
( pleine , blanc )
( pleine , blanc )
( pleine , blanc )

But this is wrong , because i should have something like this:

Couche 0
( pleine , blanc ) ( pleine , blanc ) ( pleine , blanc )

And i couldn't make it .I know that the error is because I'm making a tour in the vector with the dimension i , and i should do it the k dimension , but really I've done a lot of mixes and all the combinations that i thought andonly obtain :

Couche 0
( pleine , blanc )

Or a segmentation fault , I spend a lot of time to this exercise and i feel blocked , if you could help me that would be nice , the problemformeis in the function afficher() or with the += operator.
Finally i found the problem it was in Brique declaration , i had a <<endl; that i didn't need it.

So I'm going to mark this like resolved.
Hello,

i have to do this same work, it still two days for the due date ;(

i have written my version but it did'nt work well!
can you publish your version to have some idea and to correct my version ?
or to send me by email?

Thanks a lot.
Best regards.
Hello!

I have a problem with an exercise of the same kind (Tower of Hanoi). I know that for my exercise there are several solutions on the net but I would like to find by myself that's why I would like to have the solution for this exercise to better understand the logic of the 3D. Can you send me the last version you have ?

Thx!
Hi researcher , welll , sure I'm going to publish my version , but it doesn't work totally, the problem is in the main because of this:

toit ^= profondeur % (vide + toitG + toitD);

But if you do something like this:

toit ^= profondeur % (vide + toitG + toitD+vide);

it works perfectly.

So here is my code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258`` ``````#include #include #include using namespace std; // Pour simplifier typedef string Forme ; typedef string Couleur ; class Brique { private: Forme forme ; Couleur couleur ; public: /***************************************************** Compléter le code à partir d'ici *******************************************************/ Brique (Forme a, Couleur b):forme(a),couleur(b){} Forme get_forme()const{return forme;} Couleur get_couleur() const{return couleur;} ostream& afficher(ostream& sortie) const { if((forme ==" ")&& (couleur=="")){ sortie<<" "; } else { sortie<<"("<>> contenu; public: Construction (Brique a); unsigned int get_contenu_size()const{return contenu.size();} unsigned int get_contenu_size(int i)const{return contenu[i].size();} unsigned int get_contenu_size(int i, int j)const{return contenu[i][j].size();} Brique get_contenu(int i=0, int j=0, int k=0)const{return contenu[i][j][k];} vector>> get_contenuVect()const{return contenu;} ostream& afficher(ostream& ) const; void operator +=(Construction const &); void operator -=(Construction const &); void operator ^=(Construction const &); }; ostream& operator <<(ostream& sortie , Construction const & tmp) { return tmp.afficher(sortie); } ostream& Construction::afficher(ostream& os) const { unsigned int j(0),k(0);//i(0), if (contenu.empty()) return os; else { //for (i=0; i < contenu.size(); i++) for (size_t i(contenu.size()-1); i >= 0 && i < contenu.size(); --i) { // unsigned int i1 =contenu.size()-1-i; cout <<" Couche " << i<=hauteur1) { profondeur1=contenu[0].size();//profondeur de la construction a profondeur2=c2.contenu[0].size();//profondeur de la construction a if(profondeur2>=profondeur1) { for(unsigned int hauteur=0; hauteur=hauteur1) { //profondeur1=contenu[0].size();//profondeur de la construction a //profondeur2=c2.contenu[0].size();//profondeur de la construction a for(hauteur=0; hauteur > (1,vector (1, a))) {} /******************************************* * Ne rien modifier après cette ligne. *******************************************/ int main() { // Modèles de briques Brique toitD("obliqueD", "rouge"); Brique toitG("obliqueG", "rouge"); Brique toitM(" pleine ", "rouge"); Brique mur (" pleine ", "blanc"); Brique vide (" ", ""); unsigned int largeur(4); unsigned int profondeur(3); unsigned int hauteur(3); // sans le toit // on construit les murs Construction maison( hauteur / ( profondeur % (largeur * mur) ) ); // on construit le toit Construction toit(profondeur % ( toitG + 2*toitM + toitD )); toit ^= profondeur % (vide + toitG + toitD); // on pose le toit sur les murs maison ^= toit; // on admire notre construction cout << maison << endl; return 0; } ``````

If you can fix it tha would be amazing .
Thank you so much for help :)
Ok i will see your version.. If I find the fault, I will publish it here

i see that you function work from 4 elements:
toit ^= profondeur % (vide + toitG + toitD+4+5....);
i think that the problem is here:
const Construction operator+(Construction a,Construction const& b)

so here

void Construction::operator +=(Construction const& c2)

Bests.
Last edited on
Hello guys,
I reviewed the script that alejandrojimenez posted and I have modified it to make it works without the
 `` `` ``toit ^= profondeur % (vide + toitG + toitD+vide);``

You can see that the problem was is the method afficher

Here is the script:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223`` ``````#include #include #include using namespace std; // Pour simplifier typedef string Forme ; typedef string Couleur ; class Brique { private: Forme forme ; Couleur couleur ; public: /***************************************************** Compléter le code à partir d'ici *******************************************************/ Brique (Forme a, Couleur b):forme(a),couleur(b){} ostream& afficher(ostream& sortie) const; }; ostream& Brique::afficher(ostream& sortie) const { if(!couleur.empty()) { return sortie << "(" << forme << ", " << couleur << ") "; } else { return sortie << forme ; } } ostream& operator <<(ostream& sortie , Brique const & val) { return val.afficher(sortie); } class Construction { friend class Grader; private: vector>> contenu; public: Construction (Brique a); ostream& afficher(ostream& ) const; void operator +=(Construction const &); void operator -=(Construction const &); void operator ^=(Construction const &); }; ostream& Construction::afficher(ostream& os) const { unsigned int j(0),k(0);//i(0), if (contenu.empty()) return os; else { for (size_t i(contenu.size()-1); i >= 0 && i < contenu.size(); --i) { cout <<"Couche " << i<<" :"<=hauteur1) { profondeur1=contenu[0].size();//profondeur de la construction a profondeur2=c2.contenu[0].size();//profondeur de la construction a if(profondeur2>=profondeur1) { for(unsigned int hauteur=0; hauteur=hauteur1) { for(hauteur=0; hauteur > (1,vector (1, a))) {} /******************************************* * Ne rien modifier après cette ligne. *******************************************/ int main() { // Modèles de briques Brique toitD("obliqueD", "rouge"); Brique toitG("obliqueG", "rouge"); Brique toitM(" pleine ", "rouge"); Brique mur (" pleine ", "blanc"); Brique vide (" ", ""); unsigned int largeur(4); unsigned int profondeur(3); unsigned int hauteur(3); // sans le toit // on construit les murs Construction maison( hauteur / ( profondeur % (largeur * mur) ) ); // on construit le toit Construction toit(profondeur % ( toitG + 2*toitM + toitD )); toit ^= profondeur % (vide + toitG + toitD); // on pose le toit sur les murs maison ^= toit; // on admire notre construction cout << maison << endl; return 0; }``````

Please tell me if it helps.

Thanks
Hello,

on my machine, it works.
When i submit my code, i have:
fatal error in "test_construction_op_plus_eq": memory access violation at address: 0xfffffffffffffff8: no mapping at fault address

Thanks a lot.
Bests.
Last edited on
Hello Researcher,

The method operator+= has an issue. I think we have to adapt it because at his stage we always take the first hauteur and profondeur of the contenu, so, of the first Brique of the contenu.

I also think that operator-= will have the same issue.

I will investigate on my side... Because I am still stuck here as well.

Let us know if you find the solution.
No, i can't find a solution ! i'm blocked really!

Let me know if you find a solution please. (it still only one day)

Bests.
Your attribute "contenu" is private. Isn't it an issue with accessing to a private attribute?
I'm beginning in C++ but it seems that your error message concerns memory access so it can be a problem with pointer/references...
Topic archived. No new replies allowed.