dimanche 25 avril 2021

How does inheritance works in case of factory methods in cpp?

I'm trying to solve this simple riddle at codingames and i though i will excercies in OOP However it seems I've forgot how cpp works in this field and i got an error i do not comprehend.

/tmp/Answer.cpp:82:1: error: invalid abstract return type ‘Sign’
   82 | Sign from_str(const int value, const std::string& s)
      | ^~~~
/tmp/Answer.cpp:14:7: note:   because the following virtual functions are pure within ‘Sign’:
   14 | class Sign {
      |       ^~~~
/tmp/Answer.cpp:22:25: note:    ‘virtual std::string Sign::str() const’
   22 |     virtual std::string str() const = 0;
      |                         ^~~
/tmp/Answer.cpp:82:6: error: invalid abstract return type for function ‘Sign from_str(int, const string&)’
   82 | Sign from_str(const int value, const std::string& s)
      |      ^~~~~~~~
/tmp/Answer.cpp: In function ‘Sign from_str(int, const string&)’:
/tmp/Answer.cpp:85:26: error: cannot allocate an object of abstract type ‘Sign’
   85 |         return Rock(value);
      |                          ^
/tmp/Answer.cpp:87:27: error: cannot allocate an object of abstract type ‘Sign’
   87 |         return Paper(value);
      |                           ^
/tmp/Answer.cpp:89:30: error: cannot allocate an object of abstract type ‘Sign’
   89 |         return Scissors(value);
      |                              ^
/tmp/Answer.cpp:91:28: error: cannot allocate an object of abstract type ‘Sign’
   91 |         return Lizard(value);
      |                            ^
/tmp/Answer.cpp:93:27: error: cannot allocate an object of abstract type ‘Sign’
   93 |         return Spock(value);

And the code looks like this:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Rock;
class Paper;
class Scissors;

class Sign {
public:
    Sign(const int v): value(v) {};
    virtual ~Sign() {};
    bool operator<(const Sign& other) { return value < other.value ? false : true; }
    virtual std::string str() const = 0;

    int value{};
};

class Rock : public Sign {
public:
    Rock(const int v): Sign(v) {};
    bool operator<(const Paper& other) { return true; }
    bool operator<(const Scissors& other) { return false; }
    std::string str() const override { return "Rock"; }

};

class Paper : public Sign {
public:
    Paper(const int v): Sign(v) {};
    bool operator<(const Rock& other) { return true; }
    bool operator<(const Scissors& other) { return false; }
    std::string str() const override { return "Paper"; }
};

class Scissors : public Sign {
public:
    Scissors(const int v): Sign(v) {};
    bool operator<(const Rock& other) { return false; }
    bool operator<(const Paper& other) { return true; }
    std::string str() const override { return "Scissors"; }
};


Sign from_str(const int value, const std::string& s)
{
    if(s == "R")
        return Rock(value);
    if(s == "P")
        return Paper(value);
    if(s == "C")
        return Scissors(value);
    
    throw 1;
}

int main()
{
    int N;
    cin >> N; cin.ignore();
    std::vector<Sign> s{};

    for (int i = 0; i < N; i++) {
        int NUMPLAYER;
        string SIGNPLAYER;
        cin >> NUMPLAYER >> SIGNPLAYER; cin.ignore();
        s.emplace_back(from_str(NUMPLAYER, SIGNPLAYER));
    }
}

At this point I don't really understand why I can't use Sign as return value from the factory method that is returning concrete types and emplace it on my data pile.

And if I add to base class

virtual std::string str() const { return "Sign"; };

I will only get the base class printout.

Aucun commentaire:

Enregistrer un commentaire