使用智能指针的通用向量类

时间:2018-08-04 23:39:42

标签: c++ visual-c++ vector

我正在使用智能指针编写通用向量类。我正在改写我最初做的here。我是Visual Studio c ++的新手,我不理解这些错误:

byte[] bytes;
using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256))
{
    bytes = deriveBytes.GetBytes(PBKDF2SubkeyLength);
}

这是相关的头文件:

1>------ Build started: Project: Vector, Configuration: Debug Win32 ------
1>main.cpp
1>c:\dev\vector\vector\vector.h(124): error C2955: 'Vector': use of class template requires template argument list
1>c:\dev\vector\vector\vector.h(12): note: see declaration of 'Vector'
1>c:\dev\vector\vector\vector.h(124): error C2244: 'Vector<T>::operator =': unable to match function definition to an existing declaration
1>c:\dev\vector\vector\vector.h(124): note: see declaration of 'Vector<T>::operator ='
1>c:\dev\vector\vector\vector.h(124): note: definition
1>c:\dev\vector\vector\vector.h(124): note: 'Vector &Vector<T>::operator =(const Vector<T> &)'
1>c:\dev\vector\vector\vector.h(124): note: existing declarations
1>c:\dev\vector\vector\vector.h(124): note: 'Vector<T> &Vector<T>::operator =(Vector<T> &&) noexcept'
1>c:\dev\vector\vector\vector.h(124): note: 'Vector<T> &Vector<T>::operator =(const Vector<T> &)'
1>c:\dev\vector\vector\vector.h(131): error C2244: 'Vector<T>::operator =': unable to match function definition to an existing declaration
1>c:\dev\vector\vector\vector.h(131): note: see declaration of 'Vector<T>::operator ='
1>c:\dev\vector\vector\vector.h(131): note: definition
1>c:\dev\vector\vector\vector.h(131): note: 'Vector &Vector<T>::operator =(Vector<T> &&) noexcept'
1>c:\dev\vector\vector\vector.h(131): note: existing declarations
1>c:\dev\vector\vector\vector.h(131): note: 'Vector<T> &Vector<T>::operator =(Vector<T> &&) noexcept'
1>c:\dev\vector\vector\vector.h(131): note: 'Vector<T> &Vector<T>::operator =(const Vector<T> &)'
1>c:\dev\vector\vector\vector.h(159): error C2064: term does not evaluate to a function taking 0 arguments
1>c:\dev\vector\vector\vector.h(162): error C2382: 'Vector<T>::swap': redefinition; different exception specifications
1>c:\dev\vector\vector\vector.h(85): note: see declaration of 'Vector<T>::swap'
1>c:\dev\vector\vector\vector.h(162): error C2447: '{': missing function header (old-style formal list?)
1>c:\dev\vector\vector\vector.h(178): error C2244: 'Vector<T>::get': unable to match function definition to an existing declaration
1>c:\dev\vector\vector\vector.h(178): note: see declaration of 'Vector<T>::get'
1>c:\dev\vector\vector\vector.h(178): note: definition
1>c:\dev\vector\vector\vector.h(178): note: 'void Vector<T>::get(int) const'
1>c:\dev\vector\vector\vector.h(178): note: existing declarations
1>c:\dev\vector\vector\vector.h(178): note: 'T Vector<T>::get(int) const'
1>Done building project "Vector.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是main.cpp文件:

#ifndef Vector_h
#define Vector_h



template <class T>
class Vector {
private:

    static constexpr int initial_capacity = 100;

    // Instance variables
    int capacity = 0;
    int size = 0;
    std::unique_ptr<T> data = nullptr;

    void deepCopy(const Vector<T> &source) {
        capacity = source.size + initial_capacity;
        for (int i = 0; i < source.size; i++) {
            data[i] = source.data[i];
        }
        size = source.size;
    }

    void expandCapacity() {
        std::unique_ptr<T> oldData = std::move(data);
        capacity *= 2;
        for (int i = 0; i < size; i++) {
            data[i] = oldData[i];
        }
    }

public:

    // Constructors
    Vector() = default;                                 // empty constructor
    Vector(int n, const T &value);                      // constructor
    Vector(Vector const &vec);                          // copy constructor
    Vector<T>& operator=(Vector const &rhs);                // assignment operator

    // Rule of 5
    Vector(Vector &&move) noexcept;                     // move constructor
    Vector& operator=(Vector &&move) noexcept;          // move assignment operator
    ~Vector();                                          // destructor

    // Overload operators
    T& operator[](int index);
    T const& operator[](int index) const;
    bool operator==(const Vector &) const;

    //Vector<T>& operator+=(const Vector<T> &other) {
    //  Vector<T> newValue(size + other.size);

    //  std::copy(this->data, this->data + this->size, newValue.data);
    //  std::copy(other.data, other.data + other.size, newValue.data + this->size);

    //  newValue.swap(*this);
    //}

    friend Vector<T>& operator+(Vector<T> &source1, Vector<T> &source2) {
        int n = source1.size() + source2.size();
        static Vector<T> newSource(n);
        for (int i = 0; i < source1.size; i++) {
            newSource[i] = source1[i];
        }

        for (int i = 0; i < source2.size; i++) {
            newSource[i + soure1.size()] = source2[i];
        }

        return newSource;
    }

    friend std::ostream& operator<<(std::ostream &str, Vector &data) {
        data.display(str);
        return str;
    }

    // Member functions
    void swap(Vector &other);
    void display(std::ostream &str) const;
    int getSize() const { return size; }
    int getCapacity() const { return capacity; }
    bool empty() const { return size == 0; }
    void clear() { size = 0; }
    T get(int index) const;
    void set(int index, const T &value);
    void set(int index, T &&value);
    void insert(int index, const T &value); 
    void insert(int index, T &&value);
    void remove(int index);
    void push_back(const T &value);
    void pop_back();

};



template <class T>
Vector<T>::Vector(int n, const T &value) {
    capacity = (n > initial_capacity) ? n : initial_capacity;
    size = n;
    for (int i = 0; i < n; i++) {
        data[i] = value;
    }
}

template <class T>
Vector<T>::Vector(Vector const &vec) {
    deepCopy(vec);
}

template <class T>
Vector<T>::Vector(Vector &&move) noexcept {
    move.swap(*this);
}

template <class T>
Vector<T>& Vector<T>::operator=(Vector const &rhs) {
    Vector<T> copy(rhs);
    swap(copy);
    return *this;
}

template <class T>
Vector& Vector<T>::operator=(Vector &&move) noexcept {
    move.swap(*this);
    return *this;
}

template <class T>
Vector<T>::~Vector() {
    while (!empty()) {
        pop_back();
    }
}

template <class T>
T const& Vector<T>::operator[](int index) const {
    return data[index];
}

template <class T>
bool Vector<T>::operator==(const Vector &rhs) const {
    if (size() != rhs.size()) {
        return false;
    }
    for (int i = 0; i < size(); i++) {
        if (data[i] != rhs[i]) {
            return false;
        }
    }
    return true;
}

template <class T>
void Vector<T>::swap(Vector &other) noexcept {
    using std::swap;
    swap(capacity, other.capacity); 
    swap(size, other.size);
    swap(data, other.data);
}

template <class T>
void Vector<T>::display(std::ostream &str) const {
    for (int i = 0; i < size; i++) {
        str << data[i] << "\t";
    }
    str << "\n";
}

template <class T>
void Vector<T>::get(int index) const {
    if (index < 0 || index >= size) {
        throw std::out_of_range("[]: index out of range.");
    }
    return data[index];
}

template <class T>
void Vector<T>::set(int index, const T& value) {
    if (index < 0 || index >= size) {
        throw std::invalid_argument("set: index out of range");
    }
    data[index] = value;
}

template <class T>
void Vector<T>::set(int index, T&& value) {
    if (index < 0 || index >= size) {
        throw std::invalid_argument("set: index out of range");
    }
    data[index] = std::move(value);
}

template <class T>
void Vector<T>::insert(int index, const T& value) {
    if (size == capacity) {
        expandCapacity();
    }

    for (int i = size; i > index; i--) {
        data[i] = data[i - 1];
    }
    data[index] = value;
    size++;
}

template <class T>
void Vector<T>::insert(int index, T&& value) {
    if (size == capacity) {
        expandCapacity();
    }

    if (index < 0 || index >= size) {
        throw std::invalid_argument("insert: index out of range");
    }

    for (int i = size; i > index; i--) {
        data[i] = data[i - 1];
    }
    data[index] = std::move(value);
    size++;
}

template <class T>
void Vector<T>::remove(int index) {
    if (index < 0 || index >= size) {
        throw std::invalid_argument("insert: index out of range");
    }

    for (int i = index; i < size - 1; i++) {
        data[i] = data[i + 1];
    }
    size--;
}

template<class T>
void Vector<T>::push_back(const T& value) {
    insert(size, value);
}

template<class T>
void Vector<T>::pop_back() {
    remove(size - 1);
}

#endif /* Vector_h */

我真的不确定这里是什么问题,感谢您的帮助。

0 个答案:

没有答案