0

I have one templated class (Vector) that I am trying to get to work. I keep getting compiler such as when I try to create an object

1>Project 2.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<long>::Vector<long>(void)" (??0?$Vector@J@@QAE@XZ) referenced in function _main
    
1>Project 2.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<long>::~Vector<long>(void)" (??1?$Vector@J@@QAE@XZ) referenced in function _main

fatal error LNK1120: 2 unresolved externals

I have never worked with templated classes before and am very confused on how to get it to work properly.

Vector:

Vector.h

#ifndef VECTOR_H
#define VECTOR_H

#include "stdafx.h"

template <typename T>
class Vector
{
    private:
            // Constants 

            // The default size for the default constructor
            static const int defaultSize = 20;
        
            // Variables

            // Pointer to first element in array for this vector
            T* vectorArray;

            // Current size (size of vector array) of the array for this vector
            int vectorSize;

            // Current number of elements stored in vector
            int vectorElementCount;

    public:
            // Constructors/Deconstructor

            // Default (empty) constructor
            Vector(void);

            // Constructor with predetermined size
            Vector(int size);

            // Copy constructor
            Vector(const Vector<T>& old);

            // Deconstructor
            ~Vector();

            // Private Helper Methods

            // Doubles the amount of storage in the vector
            void expand();

            // Shifts data from a certain index to the right a set number of shifts
            void shiftRight(int index, int shifts);

            // Shifts data from a certain index to the left a set number of shifts
            void shiftLeft(int index, int shifts);

            // Public Methods

            // Returns the current size (size of vector array) of the vector
            int getVectorSize();

            // Returns the number of elements in the vector
            int getVectorElementCount();

            // Gets element at specified index
            T& getAtIndex(int index);

            // Sequentually prints all the elements stored in the vector
            void printVector();

            // Vector Manipulation Methods

            // Adds a new value to the end of the vector
            void add(T newVal);

            // Ammends the value at the specified index with the value
            void set(int index, T newVal);

            // Inserts the new value at the specified index and shifts the vector 1 shift to the right
            void insert(int index, T newVal);

            // Shifts all elements beyond one to the left, deleting the element at the index
            void deleteItem(int index);

            // Vector helper methods

            // Checks if the index exists within the vector
            bool isInBounds(int index);

            // Deletes pointers from memory. MUST BE A VECTOR OF NON-ARRAY POINTERS
            void deletePointers();

            // Deletes array pointers from memory. MUST BE A VECTOR OF ARRAY POINTERS
            void deleteArrayPointers();

            // Operator Overloaders

            T& operator[](int index);
};

#endif

Vector.cpp

#include "stdafx.h"
#include "Vector.h"

#include <iostream>


// Constructors/Deconstructor

// Default (empty) constructor
template <typename T>
Vector<T>::Vector(void)
{
    vectorElementCount = 0;
    vectorSize = defaultSize;

    vectorArray = new T[defaultSize];
}

// Constructor (non empty)
template <typename T>
Vector<T>::Vector(int size)
{
    vectorElementCount = 0;
    vectorSize = size;

    vectorArray = new T[size];
}

// Copy constructor
template <typename T>
Vector<T>::Vector(const Vector<T>& old)
{
    arraySize = old.arraySize;
    vectorElementCount = old.vectorElementCount;

    vectorArray = new T[arraySize];

    // Copies data from old to new
    for(int i=0; i < getVectorElementCount; i++) 
    {
        vectorArray[i] = old.vectorArray[i];
    }
}

// Deconstructor
template <typename T>
Vector<T>::~Vector()
{
    delete vectorArray;
}

// Private Helper Methods

// Doubles the amount of storage in the vector
template <typename T>
void Vector<T>::expand()
{
    // Create new array with 2x the size
    T* newVectorArray = new T[arraySize*2];

    // Copy the data over to the new array
    for(int i=0; i<vectorElementCount; i++)
    {
        newVectorArray[i] = vectorArray[i];
    }

    vectorSize *= 2;

    delete vectorArray;

    vectorArray = newVectorArray;
}

// Shifts data from a certain index to the right a set number of shifts
template <typename T>
void Vector<T>::shiftRight(int index, int shifts)
{
    // Expand the vector if there isn't enough room
    while(arraySize < vectorElementCount + shifts)
    {
        expand();
    }

    // Shifts and prepares for new data to occupy the spots
    for(int i=vectorElementCount - 1; i >= index; i--)
    {
        vectorArray[i + places] = vectorArray[i];
        vectorArray[i] = NULL;
    }
}

// Shifts data from a certain index to the left a set number of shifts
template <typename T>
void Vector<T>::shiftLeft(int index, int shifts)
{
    // There cannot be more shifts than there are elements
    if(vectorElementCount < shifts)
        throw 0; // TODO THROW EXCEPTION

    if(vectorElementCount == 1)
        vectorArray[0] = NULL; // There's only one item in the vector
    else
    {
        // Shift the elements down
        for(int i=index; i < (vectorElementCount - shifts); i++)
        {
            vectorArray[i] = vectorArray[i + shifts];
            vectorArray[i + shifts] = NULL;
        }
    }
}

// Public Methods

// Returns the current size (size of vector array) of the vector
template <typename T>
int Vector<T>::getVectorSize()
{
    return this.vectorSize;
}

// Returns the number of elements in the vector
template <typename T>
int Vector<T>::getVectorElementCount()
{
    return this.vectorElementCount;
}

// Gets element at specified index
template <typename T>
T& Vector<T>::getAtIndex(int index)
{
    if(index > vectorElementCount)
        throw 0; // TODO THROW EXCEPTION

    return vectorArray[index];
}

// Sequentually prints all the elements stored in the vector
template <typename T>
void Vector<T>::printVector()
{
    for(int i=0; i<getVectorElementCount; i++)
        cout << i << ". " << vectorArray[i] << endl;
}


// Vector Manipulation Methods

// Adds a new value to the end of the vector
template <typename T>
void Vector<T>::add(T newVal)
{
    // Expand if the size of the vector is exceeded
    if(vectorElementCount >= vectorSize)
        expand();

    vectorArray[vectorElementCount] = newVal;

    ++vectorElementCount;
}

// Ammends the value at the specified index with the value
template <typename T>
void Vector<T>::set(int index, T newVal)
{
    if(!isInBounds(index))
        throw 0; // TODO THROW EXCEPTION

    vectorArray[index] = newVal;
}

// Inserts the new value at the specified index and shifts the vector 1 shift to the right
template <typename T>
void Vector<T>::insert(int index, T newVal)
{
    if(!isInBounds(index))
        throw 0; // TODO THROW EXCEPTION

    // Make room for the new value
    shiftRight(index, 1);

    // An element was added
    ++vectorElementCount;

    set(index, value);
}

// Shifts all elements beyond one to the left, deleting the element at the index
template <typename T>
void Vector<T>::deleteItem(int index)
{
    // Shift over the element at the index once
    shiftLeft(int index, 1);

    // The element was removed
    --vectorElementCount;
}

// Vector helper methods

// Checks if the index exists within the vector
template <typename T>
bool Vector<T>::isInBounds(int index)
{
    return (index >= 0 && index <= (vectorElementCount - 1))
}

// Deletes pointers from memory. MUST BE A VECTOR OF NON-ARRAY POINTERS
template <typename T>
void Vector<T>::deletePointers()
{
    for (int i=0; i < vectorElementCount; i++) 
    {
        delete vectorArray[i];
    }
}

// Deletes array pointers from memory. MUST BE A VECTOR OF ARRAY POINTERS
template <typename T>
void Vector<T>::deleteArrayPointers()
{
    for (int i=0; i < vectorElementCount; i++) 
    {
        delete[] vectorArray[i];
    }
}

// Operator Overloaders
template <typename T>
T& Vector<T>::operator[](int index)
{
    return this.getAtIndex(index);
}

If someone could show me what i'm doing wrong, I'd be eternally grateful.

Note: Main.cpp has #include "Vector.h"

Community
  • 1
  • 1
strategos
  • 1
  • 1
  • Why not use vector from stl? – Marcin Feb 15 '15 at 22:48
  • @Marcin This is a class project. We have to create our own Vector and cannot use the one from STL – strategos Feb 15 '15 at 22:50
  • @Axalo I don't see how that could help me pinpoint what is wrong with the code above. I'm sure the error is something minor, but this is holding me back from completing the rest of my project :/ – strategos Feb 15 '15 at 23:02

0 Answers0