0

I've seen that similar question have been asked but it couldn't help with the problem I'm having. Since it's a bit big code, to keep post short, I'll post only things I find necessary but if you need more info, feel free to ask for more code. So, I'm trying to make a simple card game. I have this class Player with this constructor and couple of attributes.

class Player
{
public:
Player(std::string playerName_, int lifeEnergy_, int magicEnergy_, CardCollection* deck_) : playerName(playerName_), lifeEnergy(lifeEnergy_), magicEnergy(magicEnergy_), deck(deck_) { activatedCards = hand = destroyedCards = nullptr; }
private:
std::string playerName;
    int lifeEnergy;
    int magicEnergy;
    CardCollection* deck, *hand, *activatedCards, *destroyedCards;
};

And in my main.cpp I've created deck, inserted a couple of cards and after I've tried creating object of class Player and pass the following parameters:

Player p2("Name", 4000, 200, deck1);

However, Visual Studio is posting a message that "no instance of constructor Player::Player matches the argument list argument types are const char[5], int, int, CardCollection. So this "Name" is underlined, giving me an error.

What am I doing wrong? I mean, first parameter of constructor is std::string, I'm passing string in main, why does it display this error?

edit:

CardCollection has these private attributes:

private:
    struct Elem
    {
        Card* card;
        Elem* next;
        Elem(Card* card_) : card(card_), next;(nullptr) {}
        ~Elem() { delete card; }
    };

    Elem* first, *last;
    int totalCardNum;

Now, Card can't be copied (that was specified) so I deleted copy and move constructors. Since Card is part of CardCollection, I coulnd't use copy and move constructor there as well so I used pointers.

So I changed main.cpp that it matches with that constructor with pointer. This is how I created card:

Wizard* c1 = new Wizard("Wizard 1", 2, 500); // never mind the parameters, it's some card stats. This part totally works

So I did this after:

CardCollection* deck1;
deck1->addIntoCollection(c1);

Then I had error that deck1 is uninitialised. And this is the moment I lost myself totally in this. I don't know what to do at this point.

possible way out: Before I had Player, both cards and deck worked perfectly as I tested printing elements of the list and it had all cards there. Since that was last best working state, is there a way to implement Player with this all in mind?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Tony Reirdan
  • 1
  • 1
  • 4
  • 5
    Looks like `deck1` is a `CardCollection`, not `CardCollection*` (a pointer to `CardCollection`), Why do you want a pointer in the constructor? – Yksisarvinen Dec 05 '19 at 11:53
  • 2
    The error isn't to do with the string portion, VS just underlines the first parameter if it can't find a match. If you look at the error, the fourth parameter is `CardCollection`, but your constructor takes in a `CardCollection *` – ChrisMM Dec 05 '19 at 11:55
  • It seems that you need [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). `deck` doesn't point anywhere. There is no real `CardCollection` object to which `deck` could point to, so trying to access this object is Undefined Behaviour. You need to create an object of type `CardCollection` before you can use it. – Yksisarvinen Dec 05 '19 at 13:04

0 Answers0