¿Cómo inicializar un objeto de clase de Typename T?

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.

El error que consigo es Incomplete type is not allowed.


#include "my_vectorHeader.h"

using namespace std;

int main(){

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


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

using namespace std;

template<typename T> class myvector {


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


    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)

    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 []
            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;
            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;
                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;
            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++;

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


preguntado el 05 de mayo de 13 a las 19:05

Nunca usar 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. -

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 primer libro. -

I think you're a Java programer and moved to C++. there are many issues in your code. -

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. -

@MM. You are exactly right. -

2 Respuestas

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);

contestado el 05 de mayo de 13 a las 19:05

Thank you. This helps a lot. - mabramo

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).

contestado el 05 de mayo de 13 a las 20:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.