-3

For a C++ class I am taking, I am creating a Vector Library. We are not allowed to use the built in vector library, of course, and I have decided to use arrays in my 'myvector' class.

I am currently trying to test my code and I am not sure how to create an object of class myvector.

The error I get is Incomplete type is not allowed.

main.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;
    }

};
mabramo
  • 1
  • 1
  • 2
  • 2
    Never use `new` in C++ except for very exceptional circumstances and until we get `std::make_unique` in C++14. You also can't make runtime-sized arrays in C++11 or prior (though you will be able to in C++14). You have to use either a container or some form of manual memory management to keep the code conforming. – chris May 05 '13 at 18:40
  • Did your instructor ask you to do this on the first day of class? Your code contains an unusually large number of errors. Either revisit your class notes from earlier, or start by reading a good [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Praetorian May 05 '13 at 18:43
  • 1
    I think you're a Java programer and moved to C++. there are many issues in your code. – masoud May 05 '13 at 18:43
  • 2
    Seems like you lack basic background in C++. please read a good basic tutorial, which explains basic syntax, local variables, pointers, templates etc. you got none of these right. – Elazar May 05 '13 at 18:44
  • @MM. You are exactly right. – mabramo May 05 '13 at 19:17
  • @Elazar I have no idea what I'm doing. – mabramo May 05 '13 at 19:17
  • @mabramo then [this](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would be a nice place to start. – Elazar May 05 '13 at 20:05

2 Answers2

2

Declaration and implementation of templates should be in the same file(or implementation should be included too). Also this

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

is incorrect(new returns pointer) and unnecessary. Just use

myvector<int> vector(3);
awesoon
  • 32,469
  • 11
  • 74
  • 99
1

Put the class (all the code) found at my_vectorLib.cpp in your my_vectorLib.h As @soon pointed you, templates classes (unless specialized) need to be at the header file (the one you include at main.cpp which where the class is instantiated, which means the compiler generates the actual code when you use the template).

Trax
  • 1,890
  • 12
  • 15