dimanche 30 mai 2021

pure virtual method called, terminate called without an active exception

I'm trying to finish my project but when I'm trying to test my program I keep facing the same error I mentioned above. The program was running perfectly until I started testing it and I just cant figure out what causes the error.

Basically I have City class:

#pragma once

#include <ostream>

class Japan;
class Modern;
class Other;

class City{
    protected:
        int _pont;
    public:
        virtual void touristModify(Japan* t)  = 0;
        virtual void touristModify(Modern* t) = 0;
        virtual void touristModify(Other* t)  = 0;
        virtual City* instanceChange() const = 0;
        virtual std::string type() const = 0;
        void setPont(int p);
        int pont() const;
        virtual ~City(){}
        friend std::ostream &operator<<(std::ostream &os,const City &City);
};

class BadCondition: public City{
    public:
        static BadCondition* instance();
        City* instanceChange() const override;
        std::string type() const override;
        void touristModify(Japan* t) override;
        void touristModify(Modern* t) override { }
        void touristModify(Other* t) override { }
        BadCondition(){}
    private:
        static BadCondition* _instance;
};

class Avarage: public City{
    public:
        static Avarage* instance();
        City* instanceChange() const override;
        std::string type() const override;
        void touristModify(Japan* t) override { }
        void touristModify(Modern* t) override;
        void touristModify(Other* t) override;
        Avarage(){}
    private:
        static Avarage* _instance;
};

class GoodCondition: public City{
    public:
        static GoodCondition* instance();
        City* instanceChange() const override;
        std::string type() const override;
        void touristModify(Japan* t) override;
        void touristModify(Modern* t) override;
        void touristModify(Other* t) override { }
        GoodCondition(){}
    private:
        static GoodCondition* _instance;
};
 

a cpp file:

#include "tourist.h"

using namespace std;


int City::pont() const{
    return _pont;
}

void City::setPont(int p){
    if(p < 1){
        _pont = 1;
    }else if(p > 100){
        _pont = 100;
    }else{
        _pont = p;
    }
}

std::ostream &operator<<(std::ostream &os,const City &city)
{
    os << "This city has  "<< city.pont() << " and condition is :" << city.type() << endl;
    return os;
}

BadCondition* BadCondition::_instance = nullptr;

BadCondition* BadCondition::instance(){
    if(_instance == nullptr){
        _instance = new BadCondition();
    }
    return _instance;
}

void BadCondition::touristModify(Japan* t){
    t->setDb(0);
}

City* BadCondition::instanceChange() const{
    if(_pont > 33){
        return Avarage::instance();
    }else if(_pont > 67){
        return GoodCondition::instance();
    }
    return BadCondition::instance();
}

std::string BadCondition::type() const{
    return " BadCondition.";
}

Avarage* Avarage::_instance = nullptr;
Avarage* Avarage::instance(){
    if(_instance == nullptr){
        _instance = new Avarage();
    }
    return _instance;
}

void Avarage::touristModify(Modern* t){
    t->setDb(t->db() * 1.1);
}

void Avarage::touristModify(Other* t){
    t->setDb(t->db() * 1.1);
}

City* Avarage::instanceChange() const{
    if(_pont < 34){
        return BadCondition::instance();
    }else if(_pont > 67){
        return GoodCondition::instance();
    }
    return Avarage::instance();
}

std::string Avarage::type() const{
    return " Avarage.";
}
GoodCondition* GoodCondition::_instance = nullptr;
GoodCondition* GoodCondition::instance(){
    if(_instance == nullptr){
        _instance = new GoodCondition();
    }
    return _instance;
}

void GoodCondition::touristModify(Japan* t) {
    t->setDb(t->db() * 1.2);
}

void GoodCondition::touristModify(Modern* t){
    t->setDb(t->db() * 1.3);
}

City* GoodCondition::instanceChange() const{
    if(_pont < 34){
        return BadCondition::instance();
    }else if(_pont < 68){
        return Avarage::instance();
    }
    return GoodCondition::instance();
}


std::string GoodCondition::type() const{
    return " in a good condition.";
}

and my main file:

#include "tourist.h"
#include "City.h"
#include <fstream>
#include <vector>


using namespace std;

void create(const string &filename, vector<Tourist*> &tourists, City* &city,int *numberOfYears){
    ifstream f(filename);
    if(f.fail()){
        exit(1);
    }

    int startingPoint = 0;
    f >> startingPoint;
    if(startingPoint < 34){
        city = BadCondition::instance();
    }else if(startingPoint < 68){
        city = Avarage::instance();
    }else {
        city = GoodCondition::instance();
    }
    tourists.empty();
    cout << "City Starting Condition:" << city->type() << endl;
    city->setPont(startingPoint);
    f >> *numberOfYears;
    for(int i=0; i<*numberOfYears; i++){
            int b = 0;
            f >> b;
            tourists.push_back(new Japan(b));
            f >> b;
            tourists.push_back(new Modern(b));
            f >> b;
            tourists.push_back(new Other(b));
    }
    cout << endl;
}

void income(int b, City* &city){
    if(b - 10000 > 0){
        cout << "Not Enough money."<< endl;
    }else{
        cout << "Enough money" << endl;
    }
    if(b > 10000){
        b -= 10000;
        city->setPont(city->pont() + ( b / 200));
    }
    cout << endl;
}

void year(vector<Tourist*> &tourists, City* &city,int pastnumberOfYears){
        int bevetel = 0;
        cout << *city << endl;
        for(int i=pastnumberOfYears * 3; i< pastnumberOfYears*3 + 3; i++){
            tourists[i]->TouristModosit(city);
            bevetel+=tourists[i]->db();
            tourists[i]->szennyezes(city);
        }
        income(bevetel,city);
        int pont = city->pont();
        city = City->allapotValtozas();
        city->setPont(pont);
        cout << *city << endl;
}

void tourism(vector<Tourist*> &tourists, City* &city,int numberOfYears){
    for(int i=0; i<numberOfYears; i++){
        year(tourists,city,i);
    }
}

//#define NORMAL_MODE
//#ifdef NORMAL_MODE

 int main()
 {
     int numberOfYears;
     vector<Tourist*> tourists;
     City* city;
     create("input.txt",tourists, city,&numberOfYears);
     tourism(tourists,city,numberOfYears);
     cout << city->pont() << endl;
     cout << city->type() << endl;
    delete city;
    delete &tourists;
    return 0;
 } 

So whenever I start testing I get the pure virtual method error. Sample test:

TEST_CASE("1 proper year "){
    int numberOfYears;
    vector<Tourist*> tourists;
    City* city;

    SECTION("Lepusztul kezdoallapot"){
        BadConditionKENT VEGZI 1 EV UTAN
        create("input2.txt",tourists, city, &numberOfYears);
        CHECK(city->pont() == 20);
        CHECK(city->type() == " BadCondition.");
        CHECK(numberOfYears == 1);
        CHECK(tourists.size() == 3);
        tourism(tourists,city,numberOfYears);
        CHECK(city->pont() == 1);
        CHECK(city->type() == " BadCondition.");
        delete city;
        delete &tourists;
    }
}

I could really use some help abd thank you for your time and answears!

Aucun commentaire:

Enregistrer un commentaire