簡體   English   中英

在 c++ 中合並兩個 arrays 時出錯?

[英]Getting error in merging two arrays in c++?

我正在嘗試合並兩個動態 arrays 但我在合並 function 時遇到錯誤

"data': is not a member of 'List<T>"

我知道錯誤正在發生,因為 merge(const List& other) 中的給定參數是列表,但我很困惑如何訪問我的 ArrayList2,它已在 main 中的 merge function 中傳遞

我的代碼是:

#include <iostream>
using namespace std;
    template<class T>
    class List {
    public:
    // return the capacity of the list
    virtual size_t capacity() const = 0;

    // return the number of elements in the list
    virtual size_t size() const = 0;

    // inserts an element to the beginning
    virtual void push_front(const T& value) = 0;

    // adds an element to the end
    virtual void push_back(const T& value) = 0;

    // removes the last element
    virtual void pop_back() = 0;

    // removes the first element
    virtual void pop_front() = 0;

    // remove the first occurrence of an element with the given value
    virtual void remove(const T& val) = 0;

    // merges two sorted lists
    virtual void merge(const List<T>& other) = 0;

    virtual ~List() {}
};




template<class T>
class ArrayList : public List<T> 
{
private:
    T* data;
    size_t max_capacity;
    size_t num_of_element;

public:
    ArrayList() = delete;   // disable default constructor

    // constructor
    ArrayList(size_t capacity) : max_capacity(capacity), num_of_element(0) {
        data = new T[capacity];
    }

    // copy constructor
    ArrayList(const ArrayList<T>& other_list) : max_capacity(other_list.max_capacity),
        num_of_element(other_list.num_of_element) {
        data = new T[max_capacity];
        for (size_t i = 0; i < other_list.num_of_element; i++) {
            data[i] = other_list.data[i];
        }
    }

    // destructor
    virtual ~ArrayList() {
        delete[]data;
    }

    size_t capacity() const override {
        return max_capacity;
    }

    size_t size() const override {
        return num_of_element;
    }

    T& operator[](int index) {
        return data[index];
    }

    bool operator==(const ArrayList<T>& other_list) {
        // not comparing capacity as eventually array list can be made capacity irrelevant using dynamic allocation
        if (num_of_element != other_list.num_of_element) {
            return false;
        }
        for (int i = 0; i < num_of_element; i++) {
            if (data[i] != other_list.data[i]) {
                return false;
            }
        }
        return true;
    }

    void push_front(const T& value)
    {


    }

    void push_back(const T& value)
    {

        if (max_capacity > num_of_element)
        {
            num_of_element++;
            data[num_of_element - 1] = value;
        }

    }

    void pop_back()
    {




    }

    void pop_front()
    {




    }

    void remove(const T& val)
    {
        int i = 0, j;
        while (i < max_capacity)
        {
            if (data[i] == val)
            {
                for (int j = i; j < num_of_element-1; j++)
                        data[j] = data[j + 1];
                    
                if (data[i] == val && (i + 1) > num_of_element - 1)
                {
                    data[i] = {};
                    num_of_element--;
                    break;
                }


                    

                    num_of_element--;
            }
           

            else
                i++;
        }
    }
            
        


    

    void merge(const List<T>& other)
    {

        int i;
        int newsize = size() + other.size();
        T* temp = new T[newsize];
        for (i = 0; i < num_of_element; i++)
            temp[i] = data[i];
            for (int j = 0; j < other.size(); j++)
{
                temp[i] = other.data[j]; //I am getting error on this line
                i++;
}



    }

private:
    
    void shift_left_to(size_t start) {
        for (size_t i = start; i < num_of_element - 1; i++) {
            data[i] = data[i + 1];
        }
    }
};




















int main() {

    ArrayList<int> list1(3);
   list1.push_back(3);
   list1.push_back(1);
   list1.push_back(1);

    ArrayList<int> list2(2);
    list2.push_back(1);
    list2.push_back(8);

    list1.merge(list2);


   /* for (size_t i = 0; i < list1.size(); i++) 
        cout<<list1[i]<<" ";
    
    cout<<"Size:"<<list1.size()<<"  Capacity:"<<list1.capacity();*/

   
    system("pause");
    return 0;
}

據推測,您的所有具體List<T>類(例如ArrayList<T> )都將具有某種元素訪問器。 您可以使這些訪問器成為List<T>接口的一部分,並在void merge(List<T> const&)的實現中調用它們。 舉個例子:

template <class T>
class List {
   public:
    // ...
    virtual T& operator[](int index) = 0;
    virtual T const& operator[](int index) const = 0;
};

template <class T>
class ArrayList : public List<T> {
   private:
    T* data;
    size_t max_capacity;
    size_t num_of_element;

   public:
    // ...

    T& operator[](int index) override { return data[index]; }
    T const& operator[](int index) const override { return data[index]; }

    // ...

    void merge(const List<T>& other) {
        int i;
        int newsize = size() + other.size();
        T* temp = new T[newsize];
        for (i = 0; i < num_of_element; i++) temp[i] = data[i];
        for (int j = 0; j < other.size(); j++) {
            temp[i] = other[j];  // < Use your List<T>::operator[] here
            i++;
        }
    }

    // ...
};

我會說該消息非常具有描述性:List 沒有名為 data 的成員。 您應該使用 [] 運算符來訪問合並 function 中的列表元素。[] 運算符由 List 的后代實現。

temp[i] = other[j]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM