Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Unused variable - Błąd kompilacji

Ostatnio zmodyfikowano 2021-06-06 22:30
Autor Wiadomość
Creativess
Temat założony przez niniejszego użytkownika
Unused variable - Błąd kompilacji
» 2021-06-06 21:58:35
Witam
Kompilator mówi że
attackedPlayer oraz attackerPlayer
 jest nie użyte, lecz cała deklaracją wygląda tak:
C/C++
Player * attacked = dynamic_cast < Player * >( target );
Player * attackedPlayer = dynamic_cast < Player * >( attacked );
Log kompilacji:
magic.cpp: In member function ‘virtual int64_t MagicEffectClass::getDamage(Creature*, const Creature*) const’:
magic.cpp:41:16: error: unused variable ‘attackedPlayer’ [-Werror=unused-variable]
        Player* attackedPlayer = dynamic_cast<Player*>(attacked);
                ^~~~~~~~~~~~~~
magic.cpp:42:22: error: unused variable ‘attackerPlayer’ [-Werror=unused-variable]
        const Player* attackerPlayer = dynamic_cast<const Player*>(attacker);
                      ^~~~~~~~~~~~~~
W danej funkcji jest używam
attacker oraz attacked
 wiele razy, w jaki sposób inny, poprawny można to zrobić?
Głowna funkcja w której działam nad poprawą tego błędu:
C/C++
int64_t MagicEffectClass::getDamage( Creature * target, const Creature * attacker /*= NULL*/ ) const
{
   
const Monster * monster = dynamic_cast < const Monster * >( attacker );
   
Player * attacked = dynamic_cast < Player * >( target );
   
Player * attackedPlayer = dynamic_cast < Player * >( attacked );
   
const Player * attackerPlayer = dynamic_cast < const Player * >( attacker );
   
   
if( attacker && attacker->access >= 2 ) {
       
if( offensive ) {
           
return 0;
       
}
    }
   
if( attacked && !monster && attacker )
   
{
       
Tile * tile = g_game.getTile( attacked->pos );
       
House * house = tile ? tile->getHouse()
            :
NULL;
       
if( house ) {
           
if( offensive ) {
               
attacker->sendCancel( "You cannot attack on trainers." );
               
return 0;
           
}
        }
    }
   
if(( attackType != ATTACK_NONE ) &&( target->getImmunities() & attackType ) == attackType )
       
 return 0;
   
   
if(( !offensive ||( target != attacker ) ) && target->access < g_config.ACCESS_PROTECT )
   
{
       
int64_t damage =( int64_t ) random_range( minDamage, maxDamage );
       
       
if( !offensive )
       
{
           
const Monster * targetMonster = dynamic_cast < const Monster * >( target );
           
           
if( target != attacker && targetMonster
            #ifdef TR_SUMMONS
           
&& !targetMonster->isPlayersSummon()
           
#endif  //TR_SUMMONS
           
)
               
 damage = 0;
           
else
               
 damage = - damage;
           
       
}
       
else
       
{
           
if( attacker && attacker->access >= g_config.ACCESS_PROTECT )
               
 return damage;
           
           
const Monster * monster = dynamic_cast < const Monster * >( attacker );
           
if( monster )
           
{
               
//no reduction of damage if attack came from a monster
               
return damage;
           
}
           
           
const Player * targetPlayer = dynamic_cast < const Player * >( target );
           
           
if( targetPlayer ) {
               
damage =( int32_t ) floor( damage / 2.0 );
           
}
        }
       
       
return damage;
   
}
   
return 0;
}
P-178724
pekfos
» 2021-06-06 22:11:22
Usuń zmienne o których mowa, przecież komunikat mówi że chodzi o to, że nie są używane. Po co w ogóle masz ustawione traktowanie ostrzeżeń jako błędy?
P-178725
Creativess
Temat założony przez niniejszego użytkownika
» 2021-06-06 22:19:24
Mam dodaną flagę
-Wall
, Nie mogę się pozbyć tego ponieważ kompilator wskazuje następnie na brak deklaracji attacked, log:
magic.cpp:40:15: error: expected unqualified-id before ‘=’ token
        Player = dynamic_cast<Player*>(attacked);
               ^
P-178727
pekfos
» 2021-06-06 22:30:53
Usuń całą linię. W tym wypadku ona nic nie robi.

Mam dodaną flagę
-Wall
To włącza więcej ostrzeżeń, a ale wciąż ostrzeżeń. Traktowanie ich jako błędy to sprawa flagi -Werror, lub podobnej. Kompilator się w tym temacie czepia tylko niepotrzebnych rzeczy w kodzie, będzie szybciej z kompilowaniem tego projektu, jak nie będziesz musiał czyścić całego kodu. Ostrzeżenia wystarczy przeczytać i naprawić te istotne.
P-178729
« 1 »
  Strona 1 z 1