main.obj:错误LNK2019

时间:2012-10-07 20:25:16

标签: c++

  

可能重复:
  What is an undefined reference/unresolved external symbol error and how do I fix it?

我一直在

  

错误LNK2019:未解析的外部符号“public:void __thiscall DynamicArray :: put(double)”(?put @?$ DynamicArray @ N @@ QAEXN @ Z)在函数_main中引用。

我唯一一次收到此错误的时候是从DynamicArray类中调用“put”函数

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

1 个答案:

答案 0 :(得分:5)

您的标头文件显示void put(T);但未定义此类功能。 (有一个类似的void put(T&);但是close不计入链接器。)