2014-01-10 280 views
4

當完成我的遊戲原型時,我遇到了這個錯誤,我從未見過它。 我試圖在兩個.cpp文件鏈接在一起,這樣的:無法將參數1從std :: shared_ptr轉換爲<_Ty>(??)

#include <iostream> 
    #include "mage.h" 
    #include "Warrior.h" 
    #include "Rogue.h" 
    #include "CharacterType.h" 
    #include <memory> 
    #include <string> 
    #include "Inventory.h" 
    #include "blankEnemy.h" 
    #include "Enemy.h" 
    #include "Boss.h" 
    #include <cstdlib> 
    #include <ctime> 
    #include "DeathMenu.h" 
    #include "GameStart.h" 
    #include "FirstPhase.h" 
    #include <Windows.h> 
    #include "SecondPhase.h" 
    #include "PhaseOnePT2.h" 
    using namespace std; 

    int FirstPhase(blankCharacter* myCharacter, blankEnemy* myEnemy, Inventory* myInventory,  blankEnemy* myBoss) 
    { 
    //code 
    } 

要這樣: INT GameStart(){

std::shared_ptr<blankCharacter> myCharacter; 
    std::unique_ptr<blankEnemy> myEnemy; 
    std::unique_ptr<Inventory> myInventory; 
    std::unique_ptr<blankEnemy> myBoss; 




    string name; 
    int choice; 

    cout << "______________________________________________________________________________" << endl; 
    cout << "______________________________________________________________________________" << endl; 
    cout << "Your journey has only just begun" << endl; 
    cout << "______________________________________________________________________________" << endl; 
    cout << "______________________________________________________________________________" << endl; 

    cout << " Please enter player name." << endl << endl; 
    cin >> name;   
    system("cls"); 


    cout << "______________________________________________________________________________" << endl; 
    cout << "______________________________________________________________________________" << endl; 
    cout << "Please select fighting class." << endl << endl; 
    cout <<" 1 - Mage, although not the physically strongest, mages offer a healing role" << endl; 
    cout <<" 2 - Warrior, the most balanced of all the classes, excelling in durability." << endl; 
    cout <<" 3 - Rogue, for players who want to take a more creative approach to battle." << endl; 
    cout << "______________________________________________________________________________" << endl; 
    cout << "______________________________________________________________________________" << endl; 
    cin >> choice; 




    switch(choice) 
    { 
     case 1: //Mage 
      Beep(262,500); 
      myCharacter = std::unique_ptr<blankCharacter>(new Mage(20,40,60,70,100,180,60)); 
      myInventory = std::unique_ptr<Inventory>(new Inventory(3, 3,3)); 
      myEnemy = std::unique_ptr<blankEnemy>(new Enemy(150, 60, 150)); 
      myBoss = std::unique_ptr<blankEnemy>(new Enemy(200, 200, 200)); 
      //choice = FirstPhase(); 
     case 2: //Warrior 
      Beep(262,500); 
      myCharacter = std::unique_ptr<blankCharacter>(new Warrior(40,50,65,100,160,100,60)); 
      myInventory = std::unique_ptr<Inventory>(new Inventory(3, 3, 3)); 
      myEnemy = std::unique_ptr<blankEnemy>(new Enemy(150, 60, 150)); 
      myBoss = std::unique_ptr<blankEnemy>(new Enemy(200, 200, 200)); 
      choice = FirstPhase(myCharacter, myEnemy, myInventory, myBoss); 
     break; 

     case 3: //Rogue 
      Beep(262,500); 
      myCharacter = std::unique_ptr<blankCharacter>(new Rogue(30,55,70,90,160,100,100)); 
      myInventory = std::unique_ptr<Inventory>(new Inventory(3, 3, 3)); 
      myEnemy = std::unique_ptr<blankEnemy>(new Enemy(150,60,150)); 
      myBoss = std::unique_ptr<blankEnemy>(new Enemy(200, 200, 200)); 
     // choice = FirstPhase(myCharacter, myEnemy, myInventory, myBoss); 
     break; 

     default: 
     cout << "Please select a relevant value 1 to 3" << endl << endl; 
     break; 
    } 
    return 0; 

    }; 

我已經提到的第一階段();在標題中並將其放入GameStart()中;

FirstPhase.h:

#include "GameStart.h" 
    #include <string> 
    #include "CharacterType.h" 
    #include "mage.h" 
    #include "Rogue.h" 
    #include "Warrior.h" 
    using namespace std; 
    int FirstPhase(blankCharacter* myCharacter, blankEnemy* myEnemy, Inventory* myInventory, blankEnemy* myBoss); 

但是我不斷收到此錯誤:

錯誤C2664: '的第一階段':無法從 '的std :: shared_ptr的< _Ty>' 到「轉換參數1 blankCharacter *」 圍繞

choice = FirstPhase(myCharacter, myEnemy, myInventory, myBoss); 

還有,從shared_ptr的< _Ty沒有合適的轉換>爲* blankCharacter存在? 我不知道如何解決它。

+0

BTW,你應該張貼完整的錯誤。在那裏的某個地方,它也會說「_Ty」代表什麼。 – Angew

回答

9

C++ 11智能指針不提供原始指針類型的自動轉換(出於安全原因)。使用get()

choice = FirstPhase(myCharacter.get(), myEnemy.get(), myInventory.get(), myBoss.get()); 

更妙的是,改變你的函數接受引用,而不是指針(或它正確地處理傳入的一個空值?),只是在解引用調用點的指針。

1

您應該使用get()提取底層的指針或改變原型功能使用智能指針調用:

choice = FirstPhase(myCharacter.get(), myEnemy.get(), myInventory.get(), myBoss.get()); 
相關問題