1

I am trying to implement a few classes but i got the following error when compiling the codes. I have tried to removed all the redundant codes but none of them helps. I have no idea what went wrong.

Here is the error i get when compile the code:

Severity    Code    Description Project File    Line
Error   LNK2019 unresolved external symbol "public: __thiscall FullArray<int>::FullArray<int>(unsigned int)" (??0?$FullArray@H@@QAE@I@Z) referenced in function _wmain  FinancialDerivatives    C:\Users\Jeremy Nguyen\Documents\Visual Studio 2015\Projects\FinancialDerivatives\FinancialDerivatives\FinancialDerivatives.obj 1
Error   LNK1120 2 unresolved externals  FinancialDerivatives    C:\Users\Jeremy Nguyen\Documents\Visual Studio 2015\Projects\FinancialDerivatives\Debug\FinancialDerivatives.exe    1
Error   LNK2019 unresolved external symbol "public: virtual __thiscall FullArray<int>::~FullArray<int>(void)" (??1?$FullArray@H@@UAE@XZ) referenced in function _wmain  FinancialDerivatives    C:\Users\Jeremy Nguyen\Documents\Visual Studio 2015\Projects\FinancialDerivatives\FinancialDerivatives\FinancialDerivatives.obj 1

FullArray.h file:

#pragma once
#include <vector>

template <class V>
class FullArray
{
private:
    std::vector<V> m_vector;        // Use STL vector class for storage

public:
    // Constructors & destructor
    FullArray();
    FullArray(size_t size);
    FullArray(const FullArray<V>& source);
    FullArray<V>& operator = (const FullArray<V>& source);
    virtual ~FullArray();
};

FullArray.cpp file:

#include "stdafx.h"
#include "FullArray.h"


template<class V>
FullArray<V>::FullArray()
{
    m_vector = std::vector<V>(1);   // vector object with 1 element
}

template<class V>
FullArray<V>::FullArray(size_t size)
{
    m_vector = std::vector<V>(size);
}

template<class V>
FullArray<V>::FullArray(const FullArray<V>& source)
{
    m_vector = source.m_vector;
}

template<class V>
FullArray<V>& FullArray<V>::operator=(const FullArray<V>& source)
{
    // Exit if same object
    if (this == &source) return *this;

    // Call base class constructor
    //ArrayStructure<V>::operator = (source);

    // Copy the embedded vector
    m_vector = source.m_vector;

    return *this;
}

template<class V>
FullArray<V>::~FullArray()
{
}

main file:

#include "stdafx.h"
#include "FullArray.h"

int _tmain(int argc, _TCHAR* argv[])
{
    FullArray<int> tmp(5);
    return 0;
}
Jeremy Nguyen
  • 13
  • 1
  • 3

1 Answers1

1

If you compile templates, the source needs to be available at any place where they are used, so include your .cpp file at the bottom of the .h file, or include the .cpp file instead of the .h file.

Mark Jansen
  • 1,491
  • 12
  • 24