2012-10-07 138 views
-1

可能重複:
What is an undefined reference/unresolved external symbol error and how do I fix it?main.obj:錯誤LNK2019

我不斷收到

錯誤LNK2019:無法解析的外部符號「市民:無效__thiscall DynamicArray :: put(double)「(?put @?$ DynamicArray @ N @@ QAEXN @ Z)在函數_main中引用。

唯一的一次,我得到這個錯誤是當我打電話從DynamicArray類

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

不應該認爲是'無效認沽(T&)'在你寫的所有的原型.h文件?的 – Recker

+0

可能重複[什麼是未定義參考/解析的外部符號錯誤,以及如何解決呢?(http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-誤符號和 - 如何-DO-I-固定),情況4:[模板實現不可見(http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external -symbol錯誤和知識-DO-修復/ 12574417#12574417) –

+0

試着做一個SSCE後下一次。簡短,自成體系,例子。例如,嘗試擺脫所有你不叫的功能。 – CrazyCasta

回答

5

「放」 功能你的頭文件說void put(T);,但是沒有定義這樣的函數。 (有一個類似void put(T&);但收盤沒有算到連接器。)