jeudi 27 juin 2019

How to use container classes with vector and list(Inheritance)?

I am trying to implement a class with inheritance specified with operator overloading.I am new to c++ so I am facing a problem with with list and vector constructors and operators[] overriding.Any help is highly appreciated

#include <iostream>
#include <list>
#include <vector>
#include <iterator> 
using namespace std;

class container
{
public:
    virtual ~container() = default;
    virtual double& operator[](size_t idx) = 0;
    virtual const double& operator[](size_t idx) const = 0;
    virtual size_t size() const = 0;
};

class vec: container
{
    vector<double> vectr;
    vec(size_t size) :
            vectr(size, 0)
    {
    }
    ;
    double& operator[](size_t idx) override
    {
        return vectr[idx];
    }
};
class lst: container
{
    const list<double> mylist;
    lst(size_t size) :
            mylist(size, 0)
    {
    };
    const double& operator[](size_t idx) const
    {
        list<double>::iterator it = mylist.begin();
        advance(it, 5);
        return *it;
    };

};

int main()
{
    vec Avec;
    lst Blst;
    Avec(10);
    Blst(10);
    return 0;
}

Following error are occuring In member function 'virtual const double& lst::operator const': 43:55: error: conversion from 'std::list::const_iterator {aka std::_List_const_iterator}' to non-scalar type 'std::list::iterator {aka std::_List_iterator}' requested In function 'int main()':aa 52:6: error: no matching function for call to 'vec::vec()'

Aucun commentaire:

Enregistrer un commentaire