Bonjour à tous
Actuellement je code un petit comparateur d'items pour un jeu. Seulement j'ai un petit problème. J'ai deux classes, la classe Item et la classe Comparator.
item.hpp
comparator.hpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef Item_included #define Item_included #include <iostream> #include "stat.hpp" enum ItemType{ Hat, Mantle, Weapon, Weapon2hand, Boots, Amulet, Ring, Bag, Belt, Mount, Pet, Dofus, Trophy }; /// incin enum class Item { public: Item( std::string name, unsigned short level, bool isInPanoply = false, int v = 0, int i = 0, short w = 0, int we = 0, short e = 0, short f = 0, short wa = 0, short a = 0, short p = 0 , short ac = 0, short m = 0, short pr = 0, short r = 0, short in = 0, short d = 0, short dn = 0, short de = 0, short df = 0, short dw = 0, short da = 0, short dc = 0, short dp = 0 , short rn = 0, short re = 0, short rf = 0, short rw = 0, short ra = 0, short rfn = 0, short rfe = 0, short rff = 0, short rfw = 0, short rfa = 0, short apr = 0, short mpr = 0 , short apd = 0, short mdp = 0, short h = 0, short ch = 0, short t = 0, short l = 0 ); Item( Item const &item ); private: std::string const item_name; unsigned short const item_level; bool item_isInPanoply; Stat const item_stats; }; #endif // Item_included
Le problème est assez visible, le constructeur de comparator va effectuer une copie d'un vecteur d'Item, or, les attributs de items sont private. Donc faut-il que je mettent ses attributs en protected et Comparator en héritage d'Item ? Cela ne me paraît pas correct car Comparator n'est pas censé hériter d'Item vu que son rôle est de comparer des items puis de créer une panoplie d'items.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <vector> //enum Comparable{ Hat, Mantle, Belt, Weapon, Boots, Amulet, Ring, Ring2, Mount, slot1, slot2, slot3, slot4, slot5, slot6 }; #include "WeightRunes.hpp" #include "Item.hpp" struct Stuff { Stuff( Item &ht, Item &mntl, Item &blt, Item &weap, Item &bts, Item &amu, Item &rng, Item &rng2, Item &mnt, Item &slt1, Item &slt2, Item &slt3, Item &slt4, Item &slt5, Item &slt6 ) : hat{ht}, mantle{mntl}, belt{blt}, weapon{weap}, boots{bts}, amulet{amu}, ring{rng}, ring2{rng2}, mount{mnt}, slot1{slt1}, slot2{slt2}, slot3{slt3}, slot4{slt4}, slot5{slt5}, slot6{slt6}{} Item const hat; Item const mantle; Item const belt; Item const weapon; Item const boots; Item const amulet; Item const ring; Item const ring2; Item const mount; Item const slot1; Item const slot2; Item const slot3; Item const slot4; Item const slot5; Item const slot6; }; class Comparator { public: Comparator( std::vector<Item> const &items ); Comparator(); Item compare( Item const &a, Item const &b ); /// test private: //Item compare( Item const &a, Item const &b ); unsigned int score( Item const &item ); std::vector<Item> const comparator_items; //Stuff comparator_stuff; }; #endif // Comparator_included
Edit : En attendant j'ai déjà tester avec protected et comparator en héritage d'item et j'ai cette erreurs :
Si je comprend bien il a besoin du constructeur par défaut pour copier un vecteur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 D:\Code\Workspace\app151016\comparator.cpp|3|error: no matching function for call to 'Item::Item()'|?
Et je ne peux toujours pas pour accéder aux éléments protected :
En attente de vos réponses
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 D:\Code\Workspace\app151016\Item.hpp||In member function 'unsigned int Comparator::score(const Item&)':| D:\Code\Workspace\app151016\Item.hpp|26|error: 'const Stat Item::item_stats' is protected| D:\Code\Workspace\app151016\comparator.cpp|18|error: within this context| D:\Code\Workspace\app151016\Item.hpp|26|error: 'const Stat Item::item_stats' is protected| D:\Code\Workspace\app151016\comparator.cpp|19|error: within this context| D:\Code\Workspace\app151016\Item.hpp|26|error: 'const Stat Item::item_stats' is protected| D:\Code\Workspace\app151016\comparator.cpp|20|error: within this context| .... ||=== Build failed: 21 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|,
Cordialement, Disixlis.
Partager