2014-11-05 263 views
0

如何避免這些代碼循環依賴:C2061 - 循環依賴

Mechanic.cpp:

#include "stdafx.h" 
#include "Characters.h" 
#include "Monsters.h" 

using namespace characters; 
using namespace monsters; 
using namespace std; 

void character::character_atack(character const cha, monster &monst) 
{ 
    if (cha.dexterity + k(20) >= monst.defense) 
     monst.health = monst.health - cha.strength; 
} 
int k(int const max) 
{ 
    return (rand() % max); 
} 
void monster::monster_atack(character &cha, monster const monst) 
{ 
    if (monst.atack + k(20) >= cha.dexterity) 
     cha.health = cha.health - monst.damage; 
} 

Monsters.h:

#include <iostream> 
#include <string> 

namespace monsters 
{ 
    using namespace std; 

    class monster{ 
    protected: 
     string name; 
    public: 
     int atack; 
     int damage; 
     int health; 
     int defense; 

     monster(int atk, int dmg, int hp, int def) : atack(atk), damage(dmg), 
       health(hp), defense(def) {} 
     ~monster(); 

     void monster_atack(character &cha, monster const monst); 
    }; 

    class greenskins:monster{ 
     greenskins(int atk, int dmg, int hp, int def) : monster(atk, dmg, hp, def) {} 
    }; 
} 

Characters.h:

#include <iostream> 
#include <string> 
#include <vector> 

namespace characters 
{ 
    using namespace std; 

    class character{ 
    protected: 
     int level; 
     int experience; 
     string name; 
    public: 
     int health; 
     int strength; 
     int intelligence; 
     int dexterity; 

     struct position{ 
      int x; 
      int y; 
     }pos; 

     character(int str, int in, int dex) : strength(str), intelligence(in), 
       dexterity(dex), level(1), experience(0) { 
      cout << "What's your name?" << endl; 
      cin >> name; } 
     ~character(); 

     void info_character(); 
     void character_atack(character const cha, monster &monst); 
    }; 
} 

編譯器給我錯誤,如thi S:

Error 1 error C2061: syntax error : identifier 'monster' 

Error 9 error C2511: 'void monsters::monster::monster_atack(characters::character &,const monsters::monster)' : overloaded member function not found in 'monsters::monster' 
+5

這不是一個循環依賴。 – CoryKramer 2014-11-05 17:57:37

+0

@Kulis:這似乎是編譯器錯誤。你能發佈完整的源代碼嗎? – Sumeet 2014-11-05 18:00:15

+1

通過const引用將character傳遞給character_attack,以避免每次都複製整個對象。 – 2014-11-05 18:01:28

回答

3

的問題是character有一個函數,接受monster&monster有一個函數,接受character&,但你不聲明其他任何類案件。值得慶幸的是,因爲你只是傳遞類作爲參數在這兩個地方(而不是讓他們成爲會員或某事),足以在這兩個地方,以前瞻性聲明這兩個類:

// in character.h 
namespace monsters { 
    class monster; // just fwd-declare 
} 

namespace characters { 
    class character { 
     // as before 
    }; 
} 

而在其他類似文件。

[更新]此外,您只需在頭文件中引用class character中的monster,您需要將其限定爲monsters::monster

+0

我改變了它,但我仍然有https://imgur.com/ybGQDnt – Kulis 2014-11-05 18:44:29

+1

@Kulis你顯示的錯誤,但不是代碼行,這對任何人都沒有幫助。 – 2014-11-05 18:51:26

+0

https://imgur.com/SyNrqbA(35)void character_atack(const character&cha,monster&monst); (24)void monster_atack(character&cha,const monster&monst); (69)void character :: character_atack(const character&cha,monster&monst) (75)void monster :: monster_atack(character&cha,const monster&monst) – Kulis 2014-11-05 19:07:39

1

第一個錯誤來自於下面的行Characters.h

void character_atack(character const cha, monster &monst); 

您包括Characters.h到您的.cpp文件你包括Monsters.h,因此類型之前怪物尚未公佈。爲了解決這個問題,改變你的Characters.h看起來像這樣:

... //includes 
namespace monsters { 
    class monster; 
} 

namespace characters { 
    class character { 
     ... //class definition 
    } 
} 

第二個錯誤是一個不匹配的簽名。您聲明下面的方法:

void monster_atack(character &cha, monster const monst) 

但定義

void monster::monster_atack(character &cha, const monster monst) 

至少這是編譯器說的話。

我建議將簽名改爲:

void monster_atack(character &cha, const monster& monst) 

,以防止不必要的複製操作。 (取決於當然的優化)