2017-05-12 54 views
0

我寫了嘗試使用對象從一個類,函數參數的一個片段,我不斷收到錯誤對象可能是函數中的參數嗎?

In function 'int main()': 75:23: error: expected primary-expression before 'test' 75:35: error: expected primary-expression before 'Knight'

我不知道如何解決這個問題,因爲我很新到C++。

一些示例代碼如下向下:

// Example program 
#include <iostream> 
#include <string> 


     using namespace std; 

    //player class 
    class Player { 

     public: 
     //variable declaration 
     string name; 
     string classType; 
     int strength, perception, endurance, charisma, intelligence, agility, luck; 
     int id, cubes; // currency etc. 
     bool authority; 
     int inventory[20] = {}; 
     int health = 100 + ((strength * endurance) * 2); 
     //sub stat functions 
     double getPick() { 
     return ((intelligence + luck) * (agility/4)) * .01; 
     } 
     double getSneak() { 
     return (25 + (agility * 5)) * 0.01; 
     } 
     double getIntimidation() { 
     return (charisma * 10) * 0.01; 
     } 
     double getBarter() { 
     return getIntimidation(); 
     } 
     double getScience() { 
     return ((intelligence * 5)/3); 
     } 
    }; 

    //enemys 
    class enemy { 
     public: 
     //var declaration 
     string name; 
     int HP; 
     double AC; //armor class ablity to resist hits 
     int DT; //dice used to attack 
     int eid; //id for enemys (enemy id) 
     int gear[2] = {}; //gear 
     //is the enemy alive? 
     int alive() { 
     if (HP <= 0) cout << "\nThe " << name << " is dead! "; 
     return false; 
     } 
    }; 

    //fight an enemy (option 1) 
    int fightEnemy(Player player1, enemy enemy1) { 
     cout << "\n" << player1.name << " and a " << enemy1.name << "\n"; 
     return 0; 
    } 

    int main() { 

     //test 
     Player test; 
     test.name = "test"; 
     test.classType = "test"; 
     test.strength = 3; 
     test.perception = 3; 
     test.endurance = 6; 
     test.charisma = 2; 
     test.intelligence = 6; 
     test.agility = 3; 
     test.luck = 5; 
     test.id = 1; 

     test.authority = true; 
     test.cubes = 500; 

     enemy Knight; 
     Knight.name = "Knight"; 
     Knight.HP = 20; 
     Knight.AC = 0.2; 
     Knight.DT = 12; 
     Knight.eid = 3; 
     fightEnemy(Player test, enemy Knight); 
     return 0; 
    } 
+0

嘗試'fightEnemy(測試,騎士);'。然而,你的'fightEnemy()'函數應該接受對象的引用以避免產生不必要的副本:'int fightEnemy(Player const&player1,enemy const&enemy1){...' – cdhowie

回答

6

fightEnemy(播放機測試,敵騎士);

語法在這裏是錯誤的。你只需將這些變量傳遞給函數,你實質上是再次聲明它們。

應該

fightEnemy(test, Knight); 
+0

非常感謝:) –

0
fightEnemy(Player test, enemy Knight); 

fightEnemy(test, Knight); 

的變量已經被初始化。

+1

'enemy Knight; '是正確的,但對那些習慣於不同命名約定的人來說有點混亂。 – InternetAussie

+0

好的,語法突出讓我困惑。謝謝! –

+0

謝謝!!!!!! –

相關問題