0

I have two classes: SessionCardsMode and SetOfCards. SessionCardsMode takes in its constructor pointer to object of SetOfCards. When I try to create dynamically new SessionCardsMode object in SetOfCards method initializing it with this pointer I get information: "Cannot initialize type 'SessionCardsMode' with rvalue of type 'SetOfCards*'". It looks like I haven't proper constructor, but I have provided it. I don't know why it doesn't work. The problem is in SetOfCards::getSessionCards method in the first line of it. I've found that if I try to create the same object in body of class SessionCardsMode using identical statement everything works fine, but if I try to make it out of class I get the error.

//////////////////////////////SesionCardsMode.h
#pragma once
#include "Card.h"
#include "SetOfCards.h"

class SessionCardsMode
{
protected:
    SetOfCards* m_setData;
    std::forward_list<Card*> m_sessionSet;
public:
    explicit SessionCardsMode(SetOfCards* set) : m_setData(set) {};
    virtual Card* getCard();
    //allows making combination of set setup by mixing classes that derives 
    //from ModeOfSet
    void addAndShuffle(const SessionCardsMode* mode);
};

///////////////////////////////SetOfCards.h
#pragma once
#include "Card.h"
#include "SessionCardsMode.h"

class SetOfCards
{
private:
    std::vector<Card> m_cardSet;
    std::string m_setName;
public:
    SetOfCards()=default;
    explicit SetOfCards(std::string setName);
    template<typename Iter>
    SetOfCards(Iter begin, Iter end, std::string setName);
    SessionCardsMode* getSessionCards(std::vector<CreatorAndInitVal> creators);
};

////////////////////////////////////////SetOfCards.cpp
#include "SetOfCards.h"

SessionCardsMode* SetOfCards::getSessionCards(
                  std::vector<CreatorAndInitVal> m_sessionCardsCreators)
{
    SessionCardsMode* sessionCards=new SessionCardsMode(this); // error here
    return sessionCards;
}
jakub_gros
  • 31
  • 1
  • 5

1 Answers1

0

I don't understand why you don't get an error when you declare the constructor of SessionCardsMode (when you are compiling SetOfCards.cpp) - as far as I can see, at that point, SetOfCards is not defined.

Anyway, the solution to your problem is not to #include any of the headers in other headers, but to declare (not define) the other classes. So:

//////////////////////////////SesionCardsMode.h
#pragma once
class Card;
class SetOfCards;

class SessionCardsMode
{
protected:
    SetOfCards* m_setData;
    std::forward_list<Card*> m_sessionSet;
public:
    explicit SessionCardsMode(SetOfCards* set) : m_setData(set) {};
    ...
};

///////////////////////////////SetOfCards.h
#pragma once
class Card;
class SessionCardsMode;
#include <vector>   // You need this
#include <string>

class SetOfCards
{
private:
    std::vector<Card> m_cardSet;
    std::string m_setName;
public:
    SetOfCards()=default;
    explicit SetOfCards(std::string setName);
    ...
};

////////////////////////////////////////SetOfCards.cpp
#include "SetOfCards.h"  // This should always be first
#include "..."   // You'll probably need some more here.

SessionCardsMode* SetOfCards::getSessionCards(
                  std::vector<CreatorAndInitVal> m_sessionCardsCreators)
{
    SessionCardsMode* sessionCards=new SessionCardsMode(this); // Error should be fixed
    return sessionCards;
}
  • I thought that including header with the classes would did the job. Could you tell me why #include "SetOfCards.h" should always be first? – jakub_gros Nov 23 '17 at 21:54
  • Is #include "SetOfCards.h" necessary? I guess that yes, because if I haven't included it there was problem with first argument of load method. If the include is necessary should I still declare class SetOfCards? https://pastebin.com/X9z7X4AF – jakub_gros Nov 23 '17 at 22:29