Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert parameter 1 from std::shared_ptr <_Ty> (??)

Tags:

c++

While finishing up my game prototype i came across this error, i've never seen it before. I was trying to link together two .cpp files, this:

    #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
    }

To this: 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;

    };

I have referenced Firstphase(); in a header and put it in 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);

However i keep getting this error:

error C2664: 'FirstPhase' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'blankCharacter *' Around

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

There's ,no suitable conversion from shared_ptr<_Ty> to *blankCharacter exists'? I don't know how to solve it.

like image 559
MrPurple Avatar asked Jan 10 '14 12:01

MrPurple


People also ask

What happens when shared_ptr goes out of scope?

The smart pointer has an internal counter which is decreased each time that a std::shared_ptr , pointing to the same resource, goes out of scope – this technique is called reference counting. When the last shared pointer is destroyed, the counter goes to zero, and the memory is deallocated.

What happens when you move a shared_ptr?

By moving the shared_ptr instead of copying it, we "steal" the atomic reference count and we nullify the other shared_ptr . "stealing" the reference count is not atomic, and it is hundred times faster than copying the shared_ptr (and causing atomic reference increment or decrement).

Where is std :: shared_ptr defined?

If your C++ implementation supports C++11 (or at least the C++11 shared_ptr ), then std::shared_ptr will be defined in <memory> . If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++'s libstdc++).


2 Answers

C++11 smart pointers don't offer automatic conversions to and from raw pointer types (for safety reasons). Use get():

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

Better still, change your function to accept references instead of pointers (or does it correctly handle a null value passed in?) and just dereference the pointers at call site.

like image 157
Angew is no longer proud of SO Avatar answered Oct 21 '22 17:10

Angew is no longer proud of SO


You should either use get() to extract the underlying pointer or change the prototype to use smart pointers in functions calls:

choice = FirstPhase(myCharacter.get(), myEnemy.get(), myInventory.get(), myBoss.get());
like image 1
Raul Andres Avatar answered Oct 21 '22 16:10

Raul Andres