尝试编译项目时"unresolved external symbol"错误C++

"unresolved external symbol" error when trying to compile C++ project

本文关键字:external symbol 错误 C++ unresolved 编译 项目      更新时间:2023-10-16

我试图实现几个类,但我得到了编译代码时的以下错误。我试图删除所有多余的代码,但没有一个帮助。我不知道哪里出错了。

下面是我编译代码时得到的错误:

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:UsersJeremy NguyenDocumentsVisual Studio 2015ProjectsFinancialDerivativesFinancialDerivativesFinancialDerivatives.obj 1
Error   LNK1120 2 unresolved externals  FinancialDerivatives    C:UsersJeremy NguyenDocumentsVisual Studio 2015ProjectsFinancialDerivativesDebugFinancialDerivatives.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:UsersJeremy NguyenDocumentsVisual Studio 2015ProjectsFinancialDerivativesFinancialDerivativesFinancialDerivatives.obj 1

FullArray.h文件:

#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文件:

#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()
{
}
主要文件:

#include "stdafx.h"
#include "FullArray.h"
int _tmain(int argc, _TCHAR* argv[])
{
    FullArray<int> tmp(5);
    return 0;
}

如果您编译模板,则需要在使用模板的任何地方都可以获得源代码,因此将。cpp文件包含在。h文件的底部,或者包含。cpp文件而不是。h文件。