Author Topic: c++ problems :/  (Read 1126 times)

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,239
    • View Profile
    • http://www.titusgames.de
c++ problems :/
« on: 21 February 2010, 12:09:04 »
Hi, I have a little problem with typedefs:

in skill_type.h I want to define this:

Code: [Select]
typedef list<UnitParticleSystemType*> UnitParticleSystemTypes;
// =====================================================
// class SkillType
//
/// A basic action that an unit can perform
// =====================================================

class SkillType{

   
protected:
    SkillClass skillClass;
string name;
int mpCost;
    int speed;
    int animSpeed;
    Model *animation;
    SoundContainer sounds;
float soundStartTime;
public:
UnitParticleSystemTypes unitParticleSystemTypes;

....

and in unit.cpp I want to use the type "UnitParticleSystemTypes" like this:
Code: [Select]
if((!currSkill->unitParticleSystemTypes.empty())
&& (unitParticleSystems.empty()) ){
for(UnitParticleSystemTypes::iterator it= currSkill->unitParticleSystemTypes.begin(); it!=currSkill->unitParticleSystemTypes.end(); ++it){
UnitParticleSystem *ups;
ups= new UnitParticleSystem(200);
(*it)->setValues(ups);
ups->setPos(getCurrVector());
ups->setTeamNumber(getTeam());
unitParticleSystems.push_back(ups);
Renderer::getInstance().manageParticleSystem(ups, rsGame);
}
}

I get an error in this line :
for(UnitParticleSystemTypes::iterator it= currSkill->unitParticleSystemTypes.begin();it!=currSkill->unitParticleSystemTypes.end(); ++it){
...

saying
Code: [Select]
C++ ./build/i686-pc-linux-gnu/optimize/glest_game/type_instances/unit.o
glest_game/type_instances/unit.cpp: In member function 'void Glest::Game::Unit::setCurrSkill(const Glest::Game::SkillType*)':
glest_game/type_instances/unit.cpp:324: error: conversion from 'std::_List_const_iterator<Glest::Game::UnitParticleSystemType*>' to non-scalar type 'std::_List_iterator<Glest::Game::UnitParticleSystemType*>' requested

Whats wrong? how can I fix it?
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

Yggdrasil

  • Local Moderator
  • Ornithopter
  • ********
  • Posts: 408
    • View Profile
Re: c++ problems :/
« Reply #1 on: 21 February 2010, 12:24:03 »
Use const_iterator instead of iterator.
Code: [Select]
for(UnitParticleSystemTypes::const_iterator it=...

silnarm

  • Local Moderator
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: c++ problems :/
« Reply #2 on: 21 February 2010, 12:49:36 »
That will get rid of that error...

However, it will probably create a new one on,
(*it)->setValues(ups);
unless UnitParticleSystemType::setValues() is const, and with a name like that, I doubt it is!

Hacky fix,
Code: [Select]
UnitParticleSystemType *upst = const_cast<UnitParticleSystemType*>(*it);
upst->setValues(ups);

Edit:  Actually, only the pointer will be const, not what's pointed to, so just do what Yggdrasil said, you don't need this hack ;)
« Last Edit: 21 February 2010, 13:04:22 by silnarm »
Glest Advanced Engine - Code Monkey

Timeline | Downloads

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,239
    • View Profile
    • http://www.titusgames.de
Re: c++ problems :/
« Reply #3 on: 21 February 2010, 13:58:57 »
done :)
Thanks its working perfectly now ( already checked in in megaglest now )
It may be a dumb question, but whats the difference of const_iterator and  iterator?
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

silnarm

  • Local Moderator
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: c++ problems :/
« Reply #4 on: 22 February 2010, 08:27:14 »
With a const_iterator you can't modify what it references, if you have a const collection class, you must use a const_iterator, or if you are in a const method, then any members (including any collection classes) may not be modified, so again, you must use a const_iterator.
Glest Advanced Engine - Code Monkey

Timeline | Downloads

 

anything