vector push_back with object
That's because your getName() function isn't const; it doesn't guarantee that calling the function will avoid all modification of the object itself. But the fact that you're working with a const reference requires that you only call functions that are guaranteed to not modify the object. Declare/define your getName() function with a const immediately following the parameter list:
Oh, the joys of adding const-correctness to an already partially written program. :-/
std::string getName() const;//...std::string LifeForm::getName() const{ return mName;}
Oh, the joys of adding const-correctness to an already partially written program. :-/
Alright I got some errors after changing it to const.
Battle_M.obj : error LNK2019: unresolved external symbol "public: class Lifeform & __thiscall Lifeform::operator=(class Lifeform const &)" (??4Lifeform@@QAEAAV0@ABV0@@Z) referenced in function "public: class FieldSquare & __thiscall FieldSquare::operator=(class FieldSquare const &)" (??4FieldSquare@@QAEAAV0@ABV0@@Z)
FieldSquare_M.obj : error LNK2019: unresolved external symbol "public: class Lifeform & __thiscall Lifeform::operator=(class Lifeform &)" (??4Lifeform@@QAEAAV0@AAV0@@Z) referenced in function "public: void __thiscall FieldSquare::setOccupant(class Lifeform &)" (?setOccupant@FieldSquare@@QAEXAAVLifeform@@@Z)
Goblin_M.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Lifeform::getName(void)" (?getName@Lifeform@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: class Goblin & __thiscall Goblin::operator=(class Goblin &)" (??4Goblin@@QAEAAV0@AAV0@@Z)
Debug/grid slayers.exe : fatal error LNK1120: 3 unresolved externals
Now my stats and skills are held in maps. I tried making them const but I got this.
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
with
[
_Kty=std::string,
_Ty=int
]
Here is my getStat function
int Lifeform::getStat(string stat) const
{
if ( stats.find(stat) != stats.end() )
return stats[stat];
else
{
cout << "I couldn't find that stat in the stats map" << endl;
return 0;
}
}
getSkills is very similar just with skills.
Battle_M.obj : error LNK2019: unresolved external symbol "public: class Lifeform & __thiscall Lifeform::operator=(class Lifeform const &)" (??4Lifeform@@QAEAAV0@ABV0@@Z) referenced in function "public: class FieldSquare & __thiscall FieldSquare::operator=(class FieldSquare const &)" (??4FieldSquare@@QAEAAV0@ABV0@@Z)
FieldSquare_M.obj : error LNK2019: unresolved external symbol "public: class Lifeform & __thiscall Lifeform::operator=(class Lifeform &)" (??4Lifeform@@QAEAAV0@AAV0@@Z) referenced in function "public: void __thiscall FieldSquare::setOccupant(class Lifeform &)" (?setOccupant@FieldSquare@@QAEXAAVLifeform@@@Z)
Goblin_M.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Lifeform::getName(void)" (?getName@Lifeform@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: class Goblin & __thiscall Goblin::operator=(class Goblin &)" (??4Goblin@@QAEAAV0@AAV0@@Z)
Debug/grid slayers.exe : fatal error LNK1120: 3 unresolved externals
Now my stats and skills are held in maps. I tried making them const but I got this.
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
with
[
_Kty=std::string,
_Ty=int
]
Here is my getStat function
int Lifeform::getStat(string stat) const
{
if ( stats.find(stat) != stats.end() )
return stats[stat];
else
{
cout << "I couldn't find that stat in the stats map" << endl;
return 0;
}
}
getSkills is very similar just with skills.
Quote:Original post by Magmai Kai Holmlor
const Lifeform& Lifeform::operator=(const Lifeform& copy_me)
assignment operators should return non-constant reference.
@Grahf750 this his how all your assignment operators should take the form of:
foo& operator=(const foo&);
Also like said a twice already you do not need to explicitly define assignment operators & copy constructors as they are implicitly defined (compiler generated) by default to do a member-wise copy/assign. Yes it will still work with std::basic_string as it does the correct behaviour with the default, its not a pointer.
Original post by Grahf750
Now my stats and skills are held in maps. I tried making them const but I got this.
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
with
[
_Kty=std::string,
_Ty=int
]
Here is my getStat function
int Lifeform::getStat(string stat) const
{
if ( stats.find(stat) != stats.end() )
return stats[stat];
else {
cout
struct Lifeform {
typedef std::map mmap;
//.....
};
///...
int Lifeform::getStat(const string& stat) const {
mmap::const_iterator itr = stats.find(stat);
if (itr != stats.end() )
return itr->second;
else {
cerr
2. Making std::map a mutable member:
struct Lifeform {
typedef std::map mmap;
private:
mutable mmap my_map;
};
Always prefer the former.
... again i was logged out for no reason what so ever so the above post was me and for some reason has stuff i wrote missing.
assignment operators should return non-constant reference.
@Grahf750 this his how all your assignment operators should take the form of:
foo& operator=(const foo&);
Also like said twice already you do not need to explicitly define assignment operators & copy constructors as they are implicitly defined (compiler generated) by default to do a member-wise copy/assign. Yes it will still work with std::basic_string as it does the correct behaviour with the default, its not a pointer.
That is an issue with std::map's subscript operator and using it inside a constant member functions, you have two solutions:
1. Using Constant Iterators.
2. Making std::map a mutable member:
Always prefer the former.
Quote:Original post by Magmai Kai Holmlor
const Lifeform& Lifeform::operator=(const Lifeform& copy_me)
assignment operators should return non-constant reference.
@Grahf750 this his how all your assignment operators should take the form of:
foo& operator=(const foo&);
Also like said twice already you do not need to explicitly define assignment operators & copy constructors as they are implicitly defined (compiler generated) by default to do a member-wise copy/assign. Yes it will still work with std::basic_string as it does the correct behaviour with the default, its not a pointer.
Quote:Original post by Grahf750
Now my stats and skills are held in maps. I tried making them const but I got this.
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
with
[
_Kty=std::string,
_Ty=int
]
Here is my getStat function
int Lifeform::getStat(string stat) const
{
if ( stats.find(stat) != stats.end() )
return stats[stat];
else
{
cout << "I couldn't find that stat in the stats map" << endl;
return 0;
}
}
getSkills is very similar just with skills.
That is an issue with std::map's subscript operator and using it inside a constant member functions, you have two solutions:
1. Using Constant Iterators.
struct Lifeform { typedef std::map mmap; //.....};///...int Lifeform::getStat(const string& stat) const { mmap::const_iterator itr = stats.find(stat); if (itr != stats.end() ) return itr->second; else { cerr << "I couldn't find that stat in the stats map" << endl; return 0; }}
2. Making std::map a mutable member:
struct Lifeform { typedef std::map mmap; private: mutable mmap my_map;};
Always prefer the former.
The problem here is that when you do:
mmap::const_iterator itr = stats.find(stat);
it says error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>::iterator' (or there is no acceptable conversion)
mmap::const_iterator itr = stats.find(stat);
it says error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>::iterator' (or there is no acceptable conversion)
Quote:Original post by Grahf750
The problem here is that when you do:
mmap::const_iterator itr = stats.find(stat);
it says error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>::iterator' (or there is no acceptable conversion)
Hmm well i tested it out on VC++ 7.1 (that appears to be what your using) and its work fine but that error looks suspicious to me you should know that:
const std::map::iterator != std::map::const_iterator
Meaning they are not the same type.
Alright yeh fixed that I misread what you put and thought the const_Iterator was the name of the const iterator that is just stupid blindness on my part. You would think I would wonder because I never made it const lol. So that works fine.
I am all good now so thanks for the help all and especially you snk_kid.
I still got this error though and I have not explicitely defined the = operator anymore as you said.(Please read paragraph after error)
Info: I store a lifeform in the fieldSquare class
Battle_M.obj : error LNK2019: unresolved external symbol "public: class Lifeform const & __thiscall Lifeform::operator=(class Lifeform const &)" (??4Lifeform@@QAEABV0@ABV0@@Z) referenced in function "public: class FieldSquare & __thiscall FieldSquare::operator=(class FieldSquare const &)" (??4FieldSquare@@QAEAAV0@ABV0@@Z)
FieldSquare_M.obj : error LNK2001: unresolved external symbol "public: class Lifeform const & __thiscall Lifeform::operator=(class Lifeform const &)" (??4Lifeform@@QAEABV0@ABV0@@Z)
Debug/grid slayers.exe : fatal error LNK1120: 1 unresolved externals
Now I commented some stuff out in the field square class dealing with the lifeform. Just the set and get methods and when I uncommented it and tried compiling again it worked. I have tested it and it works fine I find this strange anybody have any explanation? Maybe it wasn't recompiling or just comfused I have had that happend before where restarting visual studio makes it work.
I am all good now so thanks for the help all and especially you snk_kid.
I still got this error though and I have not explicitely defined the = operator anymore as you said.(Please read paragraph after error)
Info: I store a lifeform in the fieldSquare class
Battle_M.obj : error LNK2019: unresolved external symbol "public: class Lifeform const & __thiscall Lifeform::operator=(class Lifeform const &)" (??4Lifeform@@QAEABV0@ABV0@@Z) referenced in function "public: class FieldSquare & __thiscall FieldSquare::operator=(class FieldSquare const &)" (??4FieldSquare@@QAEAAV0@ABV0@@Z)
FieldSquare_M.obj : error LNK2001: unresolved external symbol "public: class Lifeform const & __thiscall Lifeform::operator=(class Lifeform const &)" (??4Lifeform@@QAEABV0@ABV0@@Z)
Debug/grid slayers.exe : fatal error LNK1120: 1 unresolved externals
Now I commented some stuff out in the field square class dealing with the lifeform. Just the set and get methods and when I uncommented it and tried compiling again it worked. I have tested it and it works fine I find this strange anybody have any explanation? Maybe it wasn't recompiling or just comfused I have had that happend before where restarting visual studio makes it work.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement