-1

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I keep getting

error LNK2019: unresolved external symbol "public: void __thiscall DynamicArray::put(double)" (?put@?$DynamicArray@N@@QAEXN@Z) referenced in function _main.

The only time I get this error is when I call the "put" function from the DynamicArray class

DynamicArray.h

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <new>
#include <cstdlib>
using namespace std;

template <class T>
class DynamicArray
{
    private:
        T *origin;
        T *allocator;
        int size;
        int current;


    public:
        DynamicArray();
        DynamicArray(int);
        DynamicArray(const DynamicArray&);
       ~DynamicArray();
        void add(int);
        void erase(int);
        void empty();
        void put(T);
        void remove();
        int getSize();
        T &operator[](const int&);
        const T operator=(const DynamicArray&);
        void put(T&);
};
template <class T>
DynamicArray<T>::DynamicArray()
{
    origin = new T[5];
    allocator = NULL;
    size = 5;
    current = 0;
}

template <class T>
DynamicArray<T>::DynamicArray(int s)
{
    size = s;

    try
    {
        origin = new T[s];
        allocator = NULL;
    }
    catch(bad_alloc)
    {
        cout << "Error: Bad memery allocation\n";
        exit(EXIT_FAILURE);
    }
}

template <class T>
DynamicArray<T>::DynamicArray(const DynamicArray& obj)
{
    empty();
    for(int counter = 0; counter < obj.size - 1; counter++)
    {
         origin[counter] = obj.origin[counter];
    }
}

template <class T>
DynamicArray<T>::~DynamicArray()
{
    delete [] origin;
    delete [] allocator;
    size = NULL;
}

template <class T>
void DynamicArray<T>::add(int size)
{
    allocator = new T[size];
    for(int counter = 0; counter < this-> size - 1; counter++)
    {
        allocator[counter] = origin[counter];
    }
    origin = NULL;
    origin = new T[size];
    this->size = this->size + size;

    for(int counter = 0; counter < size - 1; counter++)
    {
         origin[counter] = allocator[counter];
    }
    allocator = NULL;
}

template <class T>
void DynamicArray<T>::erase(int ammount)
{
    if(ammount - size > size)
        throw "\nnegetive memory location error\n";

    allocator = new T[size - ammount];

    for(int counter = 0; counter < this-> size - ammount - 1; counter++)
    {
        allocator[counter] = origin[counter];
    }

    origin = NULL;
    size = size - ammount;
    origin = new T[size];

    for(int counter = 0; counter < size - 1; counter++)
    {
        origin[counter] = allocator[counter];
    }
    allocator = NULL;
}

template <class T>
void DynamicArray<T>::empty()
{
    if(size >= 0)
        delete [] origin;
}


template <class T>
void DynamicArray<T>::remove()
{
     erase(1);
}


template <class T>
int DynamicArray<T>::getSize()
{
     return size;
}

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


template <class T>
const T DynamicArray<T>::operator=(const DynamicArray &obj)
{
    size = obj.size;
    delete [] origin;

    origin = new T[size];

    for(int counter = 0; counter < size; counter++)
        *(origin + counter) = *(obj.origin + counter);

    return *origin;
}

template <class T>
void DynamicArray<T>::put(T &obj)
{
    if(current == size - 1)
    {

        add(1);
        origin[size - 1] = obj;
        ++current; 
    }
    else
    {
        origin[current] = obj;
        ++current;
    }
}

#endif // !DYNAMICARRAY_H

main.cpp

#include <iostream>
#include "DynamicArray.h"

using namespace std;

int main()
{
    DynamicArray<double> a(5);

    a.put(1);

    system("pause");
    return 0;
}
Community
  • 1
  • 1
user1721803
  • 1,125
  • 2
  • 14
  • 21
  • Shouldnt that be `void put(T &)` in the .h file where you write all the prototypes? – Recker Oct 07 '12 at 20:29
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix), case 4: [Template implementations not visible](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574417#12574417) – Raymond Chen Oct 07 '12 at 20:36
  • Try making a SSCE post next time. Short, Self Contained, Example. For instance, try getting rid of all the functions you don't call. – CrazyCasta Oct 07 '12 at 20:42

1 Answers1

5

Your header file says void put(T); but no such function was ever defined. (There is a similar one void put(T&); but close doesn't count to the linker.)

sehe
  • 374,641
  • 47
  • 450
  • 633
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135