jeudi 5 novembre 2020

Descendants of parents class unable to acess to private member data fields of its host class even after declaring it friend [duplicate]

I am creating the state behavior pattern for stack(Uml diagram is below). I made StackState class as a friend in order for its descendants to access the private data fields of Stack. In this case, EmptyState(Which is a subclass of StackState) cannot access the private data fields inside Stack even after I declared stack state as my friend. It seems to throw a compile time error C2248 for visual studio compilers and I have no idea what is causing it. I have tried everything but making the data fields of Stack public because it defeats the purpose of making stack state as a friend class( for which I want its descendants to be able to access the private data fields of Stack). I also had to omit the other subclasses of Stackstate in order to generate the minimum amount of code to produce the error. So when I push into the stack and tried to change the state, this code only contains the empty state. So in essence, I want emptyState to access the private data fields of Stack.

In stack.hpp

#include <iostream>
#include <algorithm>
#include <vector>
#include <memory>
#ifndef  STACKHPP
#define  STACKHPP
template<typename T>
class StackState;

template<typename T>
class Stack
{
private:
    std::vector<T>data;
    int top_index;
    std::shared_ptr<StackState<T>>state;
    void Init(int size);
private:
    
    friend class StackState<T>;
    void changeState(const std::shared_ptr<StackState<T>> source);
    //int get_top_index() const ;
    //std::vector<T>& get_data() const;
public:
    
    Stack();                    //Defualt constructor
    Stack(int size);
    Stack(const Stack <T>& source); //Copy constructor
    Stack(Stack <T>&& target);      //Move constructors
    Stack <T>& operator=(const Stack <T>& source);
    Stack <T>& operator=(Stack&& target);   //Move semantics
    
    void Push(const T& data);
    T& Pop();
};
#ifndef STACKCPP
#include "Stack.cpp"
#endif // !STACKCPP
#endif

And in Stack.cpp

#include "Stack.hpp"
#include "StackState.hpp"
#include "EmptyState.hpp"
#ifndef  STACKCPP
#define  STACKCPP
template<typename T>
Stack<T>::Stack()
{
    Init(1);
}
template<typename T>
Stack<T>::Stack(int size)
{
    Init(size);
}
template<typename T>
Stack<T>::Stack(const Stack<T>& source) : data(source.data), top_index(source.top_index), state(source.state)
{
}
template<typename T>
Stack<T>::Stack(Stack<T>&& target) : data(std::move(target.data)), top_index(std::move(target.top_index)), state(std::move(target.state))
{
}
template<typename T>
Stack<T>& Stack<T>::operator=(const Stack<T>& source)
{
    if (this == &source)
    {
        return *this;
    }
    data = source.data;
    top_index = source.top_index;
    state = source.state;
    return *this;
    // TODO: insert return statement here
}
template<typename T>
Stack<T>& Stack<T>::operator=(Stack&& target)
{
    if (this == &target)
    {
        return *this;
    }
    data = std::move(target.data);
    top_index = std::move(target.top_index);
    state = std::move(target.state);
    return *this;
}
template<typename T>
void Stack<T>::changeState(const std::shared_ptr<StackState<T>> source)
{
    state = source;
}
template<typename T>
void Stack<T>::Init(int size)
{
    if (size < 1)
        data = std::vector<T>(1);
    else
        data = std::vector<T>(size);
    top_index = 0;
    
    state = std::make_shared<EmptyState<T>>();
}
template<typename T>
void Stack<T>::Push(const T& data)
{
    state->Push(*this, data);
}
template<typename T>
T& Stack<T>::Pop()
{
    return state->Pop(*this);
}
#endif // ! StackCPP

And in the StackState.hpp

#include "Stack.hpp"
#ifndef  STACKSTATE_HPP
#define  STACKSTATE_HPP
template<typename T>
class Stack;
template<typename T>
class StackState
{

public:
    virtual T& Pop(Stack<T>& data);
    virtual void Push(Stack<T>& dat, const T& data);
};
#ifndef STACKSTATE_CPP
#include "StackState.cpp"
#endif // !STACKSTATE_CPP
#endif // ! STACKSTATE_HPP

And also in StackState.cpp

#include "StackState.hpp"

#ifndef  STACKSTATE_CPP
#define  STACKSTATE_CPP
template<typename T>
T& StackState<T>::Pop(Stack<T>& data)
{
    data.top_index--;
    return data.data[data.top_index];
}

template<typename T>
void StackState<T>::Push(Stack<T>& data, const T& data_m)
{
    data.data.push_back(data_m);
    data.top_index++;
}

#endif // ! STACKSTATE_CPP

And then finally the EmptyStack.hpp and EmptyStack.cpp. The main file has been included

//#include "NotFullEmptyState.hpp"
//#include "FullState.hpp"
#include "StackState.hpp"
#ifndef EMPTYSTATE_HPP
#define EMPTYSTATE_HPP
template<typename T>
class EmptyState :public StackState<T>
{
public:
    virtual T& Pop(Stack<T>& data) override;
    virtual void Push(Stack<T>& dat, const T& data) override;
};

#ifndef EMPTYSTATE_CPP
#include "EmptyState.cpp"
#endif // !STACKSTATE_CPP
#endif // ! STACKSTATE_HPP
#include "EmptyState.hpp"
#ifndef EMPTYSTATE_CPP
#define EMPTYSTATE_CPP

template<typename T>
T& EmptyState<T>::Pop(Stack<T>& data)
{
    throw std::exception("You cannot pop an empty stack");
}

template<typename T>
void EmptyState<T>::Push(Stack<T>& dat, const T& data)
{

    StackState<T>::Push(dat,data);
//This is whats generating the error.The condition below.
    **if (dat.top_index == dat.data.size())
        dat.changeState(std::make_shared<EmptyState<T>>());**
    else
        dat.changeState(std::make_shared<EmptyState<T>>());
}

#endif // !1

And lastly the main

#include "Stack.hpp"
int main()
{
    try
    {
        //Reserve 5 elements on the stack 
        Stack<int>data(5);
        data.Push(15);
        data.Push(14);
        data.Push(11);
        data.Push(16);
        data.Push(25);
        std::cout << data.Pop() << std::endl;
        std::cout << data.Pop() << std::endl;
        std::cout << data.Pop() << std::endl;
        std::cout << data.Pop() << std::endl;
        std::cout << data.Pop() << std::endl;
    }
    catch (const std::exception& error)
    {
        std::cout << error.what() << std::endl;
    }
}

And lastly for those curious about the design pattern, here is the state UML transition diagram it follows. I did not include the fullstate and the notFullEmptyState in order to generate the minimum amounts of code.The state transition diagram

Aucun commentaire:

Enregistrer un commentaire