-3

对于我正在学习的 C++ 课程,我正在创建一个向量库。当然,我们不允许使用内置的向量库,我决定在我的“myvector”类中使用数组。

我目前正在尝试测试我的代码,但我不确定如何创建 myvector 类的对象。

我得到的错误是Incomplete type is not allowed.

主.cpp:

#include "my_vectorHeader.h"

using namespace std;

int main(){

myvector<int> vector = new myvector<int>(3);
}

my_vectorLib.cpp:

#include "my_vectorHeader.h"
#include iostream
#include exception

using namespace std;

template<typename T> class myvector {

private:

    int vector_size, //holds current size
        vector_capacity; //holds current capacity

public:

    //constructors
    myvector<T>(int length){
        T vector[length];
        vector_size = length;
        vector_capacity = length;
    }

    myvector<T>(const T& doppelganger){
        T vector = doppelganger;

        while(vector[vector_size] != NULL)
            vector_size++;
    }

    myvector<T>(const T& dat_arr, int arr_size){

        vector_size = arr_size;
        vector_capacity = arr_size;

        for(int i = 0; i < arr_size; i++)
            vector[i] = dat_arr[i];
    } 

    ~myvector(){ //destructor
        delete [] vector;
        delete myvector;
    }

    myvector& myvector::operator[](int index){ //override []
        try{
            throw vector[index];
        } catch(exception e){
            cout << e.what() << endl;
        }
        return vector[index];
    }

    T at(int index){
        return vector[index];
    }

    int size(){
        return vector_size;
    }

    int capacity(){
        return vector_capacity;
    }

    bool empty(){
        if(size() > 0)
            return false;
        else 
            return true;
    }

    void resize(int new_size){
        T vector_copy[new_size];

        for(int i = 0; i < new_size; i++){

            if(i >= vector_size)
                vector_copy[i] = NULL;
            else
                vector_copy[i] = vector[i];

            if(new_size > capacity)
                capacity = new_size;
        }

        vector_size = new_size;

        delete [] vector;

        T *vector;
        vector = vector_copy;

        for(int i = 0; i < vector_size; i++)
            vector[i] = vector_copy[i];

        delete [] vector_copy;
    }


    void reserve(int new_capacity){
        if(new_capacity < vector_size){
            cout << "Error. Newly provided vector capacity is smaller than the current vector size. The capacity is " << vector_capacity << " and has not been changed.";
        } else {
            vector_capacity = new_capacity;
        }
    }

    T pop_back(){
        T return_val = vector[size() - 1];
        resize(size() - 1);
        return return_val;
    }

    void push_back(T new_val){
        resize(size() + 1);
        vector[size() - 1] = new_val;
    }

    void assign(int position, T new_val){
        vector[position] = new_val;
    }

    void clear(){
        for(int i = 0; i < size(); i++)
            vector[i] = NULL;
    }

    void erase(int position){
        vector[position] = NULL;
        do{
            vector[position] = vector[position + 1];
        }while(position != NULL);
        resize(size() - 1);
    }

    void erase(int in, int between){
        int i = in + 1, j = between;
        while(i < between){
            vector[i] = vector[j];
            vector[j] = NULL;
            i++, j++;
        }
        resize(between);
    }

    void insert(int index, T new_val){
        for(int i = size + 1; i > index; i--)
            vector[i] = vector[i - 1];
        vector[index] = new_val;
    }

};
4

2 回答 2

2

模板的声明和实现应该在同一个文件中(或者实现也应该包含在内)。还有这个

myvector<int> vector = new myvector<int>(3);

不正确(new返回指针)且不必要。只需使用

myvector<int> vector(3);
于 2013-05-05T18:39:44.307 回答
1

将在 my_vectorLib.cpp 中找到的类(所有代码)放入 my_vectorLib.h 正如@soon 指出的那样,模板类(除非专门)需要位于头文件中(您在 main.cpp 中包含的那个类在哪里是实例化的,这意味着编译器会在您使用模板时生成实际代码)。

于 2013-05-05T19:00:29.367 回答