Невозможно создать экземпляр абстрактного класса и не может преобразовать аргумент 1 из const size_t в const BDictionary & 'Ошибки - PullRequest
1 голос
/ 29 марта 2020

Я использую Visual Studio 2017 в соответствии с инструкциями для этого проекта.

Я получил четыре ошибки для своей программы, которую я исследовал в течение нескольких дней, и независимо от того, что я сделал, я не могу получить эти ошибки исчезают.

Я попытался создать несколько конструкторов в BDictionary, я проверил все имена функций, и нет опечаток, и я просмотрел этот код с другими людьми и сравнил их версии, чтобы понять, почему это получает эти ошибки. Я беспокоюсь только о том, чтобы запустить его в данный момент, так как я могу выяснить, какие функции не работают оттуда. Любая и вся помощь будет оценена.

bagtestmain. cpp -Этот файл не может быть изменен

#include <string>
#include <sstream>
#include "ABag.h"
#include "BDictionary.h"

using namespace std;

const size_t DICTIONARY_SIZE = 20;

void PauseScreen(); //Used to pause screen output

/*
* Tests BDictionary with int and string objects only.
*/
int main(int argc, char** argv) {

    cout << "Student Name -- CSIS 215 Programming Assignment 1 -- Bag Dictionary" << endl << endl;

    BDictionary<int, string> myIntStrDict(DICTIONARY_SIZE); 
//Error C2259 'BDictionary<int,std::string>': cannot instantiate abstract class 27  

//Error C2664   'BDictionary<int,std::string>::BDictionary(const BDictionary<int,std::string> &)': cannot convert argument 1 from 'const size_t' to 'const BDictionary<int,std::string> &'  27  

    BDictionary<string, int> myStrIntDict(DICTIONARY_SIZE); 
//Error C2259   'BDictionary<std::string,int>': cannot instantiate abstract class   28  

//Error C2664   'BDictionary<std::string,int>::BDictionary(const BDictionary<std::string,int> &)': cannot convert argument 1 from 'const size_t' to 'const BDictionary<std::string,int> &'  28  

Я включил две ошибки, которые есть в каждой строке .

ABag.h

#ifndef ABAG_H
#define ABAG_H

#include "book.h"
#include "bagADT.h"
#include <string>
#include <stdlib.h>


template <typename E>
class ABag : public Bag<E> {
private:
    int position, capacity;
    const int max = 10;
    E* data;
public:

    // constructors/destructor
    ABag()
    {
        max = defaultSize;
        capacity = position = 0;
        data = new E[max];
    }

    ~ABag()
    {
        delete[] data;
    }

    // Insert a new item into the bag -- return false if fails and true if
    // successful
    bool addItem(const E& item)
    {
        if (bagCapacity < 10)
        {
            position++;
            data[position]; //incorrect
            capacity++;
            return true;
        }
        else
        {
            return false;
        }
    }

    // Looks for 'item' in the bag and if found updates 'item' with the 
    // bag value and returns true.  Otherwise 'item' is left unchanged
    // and the method returns false.
    bool remove(E& item)
    {
        if (position == -1) // check if bag is full
        {
            return false;
        }
        else
        {
            for (int i = position; i >= 0; i--) // loop through each object top to bottom
            {
                if (data[i] == item) // if the values are equal, then return the item
                {
                    item = data[i];
                    position--;
                    for (int j = i; j <= position; j++)
                    {
                        data[j] = data[j + 1]; // shift down the objects on top
                    }
                }
            }
            return true;
        }
    }

    // Removes the top record from the bag, puts it in returnValue, and
    // returns true if the bag is not empty.  If the bag is empty the 
    // function returns false and returnValue remains unchanged.
    bool removeTop(E& returnValue)
    {
        if (inspectTop(returnValue))
        {
            return false;
        }

        position--; //return
        return true;
    }

    // Finds the record using returnValue and if the record is found updates
    // returnValue based on the contents of the bag and returns true.  If the
    // record is not found the function returns false.  Works just like remove()
    // except that the found record is not removed from the bag.
    bool find(E& returnValue) const
    {
        if (position == -1)
        {
            return false;
        }
        else
        {
            for (int i = position; i >= 0; i--) // loops through objects and returns value
            {
                if (data[i] == returnValue)
                {
                    returnValue = data[i]; // returns the KVpair found
                    return true;
                }
            }
        }
        return false;
    }

    // Inspect the top of the bag.  If the bag is empty return
    // false and leave 'item' unchanged; otherwise, return true and update 
    // 'item' with the contents of the bag.
    bool inspectTop(E& item) const
    {
        if (position == -1)
        {
            return false;
        }
        else
        { 
            item = data[position];
            return true;
        }
    }

    // empties the bag
    void emptyBag()
    {
        position = -1; // Moves the position down to -1, in a sense removing the elements
    }

    // use the += operator to add an item to the bag
    bool operator+=(const E& addend)
    {
        return addItem(addend); // No need to rewrite addItem, just call it from here
    }

    // get the size of the bag
    int size() const
    {
        return position + 1; // returns the actual size
    }

    // get the capacity of the bag
    int bagCapacity() const
    {
        return capacity;
    }
    // bag methods: addItem, remove, operator+=, size, etc.
};

#endif  /* ABAG_H */

BDictionary.h

#ifndef BDICTIONARY_H
#define BDICTIONARY_H

#include "ABag.h"
#include "dictionaryADT.h"
#include "kvpair.h"
#include "book.h"
#include <string>

using namespace std;

template <typename Key, typename E>
class BDictionary : public Dictionary<int, string>{
public:

    // constructors/destructor
    BDictionary()
    {
        max = defaultSize;
        capacity = -1;
        position = -1;
        dictionary = new E[max];
    }          // Default constructor

    BDictionary(const Key& k, const Key& e)
    {
        max = defaultSize;
        capacity = position = 0;
    }

    ~BDictionary() 
    {
        delete[] data.
    } // Base destructor

    // Reinitialize dictionary
    void clear()
    {
        dictionary->emptyBag(); // Calls ABag's emptyBag
    }

    // Insert a record
    // k: The key for the record being inserted.
    // e: The record being inserted.
    // Return true if insert is successful and false otherwise
    bool insert(const Key& k, const E& e)
    {
        KVpair<int, string> item(k, e);
        if (dictionary->addItem(item))
        {
            return true;

        }
        else
        {
            return false;
        }
    }

    // Looks for a record using the key and if found does the following:
    // - updates the E& rtnVal
    // - removes the record from the dictionary
    // - returns true
    // If the record is not found the function returns false.
    bool remove(const Key& k, const E& rtnVal)
    {
        KVpair<int, string> item(k, rtnVal);
        if (dictionary->remove(item))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Takes an arbitrary record from the dictionary and does the following:
    // - updates the E& returnValue
    // - removes the record from the dictionary
    // - returns true
    // If the dictionary is empty the function returns false.
    bool removeAny(E& returnValue)
    {
        /*Key* k;
        k = new Key();
        KVpair<Key, E> fill(*k, returnValue);
        if (dictionary->removeTop(fill)) // Use of removeTop since we can delete ANY arbitrary element
        {
            returnValue = fill.value();
            delete k;
            return true;
        }
        else
        {
            delete k;
            return false;
        }*/
        KVpair<int, string> item(0, "");
        if (dictionary->removeTop(item))
        {
            //cout << "REMOVEANY: Removed key " << item.key() << ", which contained the value " << item.value() << endl;
            return true;
        }
        else
        {
            return false;
        }
    }

    // Looks for a record using the key and if found does the following:
    // - updates the E& returnValue
    // - returns true
    // If the record is not found the function returns false.
    bool find(const Key& k, E& returnValue) const
    {
        KVpair<int, string> item(k, returnValue);
        if (dictionary->find(item))
        {
            //std::cout << "FIND: The value at key " << item.key() << " is " << item.value() << endl;
            return true;
        }
        else
        {
            return false;
        }
    }

    // Return the number of records in the dictionary.
    int size()
    {
        return dictionary->size(); // calls size function from ABag
    }

    // methods: clear, insert, remove, removeAny, find, size, etc.

private:
    //Pointer to a ABag object.  You'll need to instantiate the bag in your constructor:
    // dictionary = new ABag<KVpair<Key, E>>(size) or something similar depending on how
    // you've implemented your ABag constructor(s).
    //This pointer gives you access to the bag which stores your data and provides the 
    //functions you need to build your dictionary.
    ABag<KVpair<int, string>>* dictionary;
    int capacity, position;
    const size_t max;
};


#endif  /* BDICTIONARY_H */
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...