ERROR LNK2019:未解析的外部符号,c ++

Nat*_*ten 2 c++ linker-errors visual-studio-2010 lnk2019

我已经编写了一个模板类,用于地图/字典数据结构,并且不断收到这个奇怪的错误(ERROR LNK2019:未解析的外部符号)

码:

AssArray.h:

#pragma once   
template <class K,class D>   
class AssArray   
{   
    int _size;   
    int _position;   
    D* _data;   
    K* _key;  

public:    
     AssArray(int);
    ~AssArray(void);

    const D& operator [](K k) const;
    D& operator [](K k);
};
Run Code Online (Sandbox Code Playgroud)

AssArray.cpp:

#include "StdAfx.h"
#include "AssArray.h"

template <class K,class D>
AssArray<K,D>::AssArray(int size)
{
    _size=size;
    _data = new D[size];
    _key = new K[size];
    _position=0;
}

template <class K,class D>
AssArray<K,D>::~AssArray(void)
{
        delete[] _data;
    delete[] _key;
}

template <class K,class D>
const D& AssArray<K,D>::operator [](K k) const
{
    //Get
    for(int i=0;i<_position;i++)
        if(_key[i]==d)
            return _data[i];
    return NULL;
}

template <class K,class D>
D& AssArray<K,D>::operator [](K k)
{
    //Set
    for(int i=0;i<_position;i++)
        if(_key[i]==d)
            return _data[i];

    if(_position<_size-1)
    {
        _key[position]=d;
        _position++;
        return _data[_position];
    }
    else
    {
        //Implement error handling
    }

}
Run Code Online (Sandbox Code Playgroud)

错误是:
1

"Error  4   error LNK1120: 3 unresolved externals   
C:\Users\*****\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\Debug
\OOPLAB4NYARE.exe   1   1   OOPLAB4NYARE"
Run Code Online (Sandbox Code Playgroud)

2

Error   1   error LNK2019: unresolved external symbol "public: __thiscall 
AssArray<char *,float>::~AssArray<char *,float>(void)" (??1?$AssArray@PADM@@QAE@XZ)
referenced in function _wmain   C:\Users\*****\Documents\Visual Studio
2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj    OOPLAB4NYARE
Run Code Online (Sandbox Code Playgroud)

3

Error   3   error LNK2019: unresolved external symbol "public: __thiscall
AssArray<char *,float>::AssArray<char *,float>(int)" (??0?$AssArray@PADM@@QAE@H@Z) 
referenced in function _wmain   C:\Users\*****\Documents\Visual Studio 
2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj    OOPLAB4NYARE
Run Code Online (Sandbox Code Playgroud)

4

Error   2   error LNK2019: unresolved external symbol "public: float & 
__thiscall AssArray<char *,float>::operator[](char *)" 
(??A?$AssArray@PADM@@QAEAAMPAD@Z) referenced in function _wmain C:\Users\Jonathan
\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj   
OOPLAB4NYARE
Run Code Online (Sandbox Code Playgroud)

我正在使用Microsoft visual studio 2010 Ultimate.这很容易让我很容易忽视.

我曾尝试清理 - >重建,创建一个新项目并复制粘贴相关代码,以及尝试找到解决方案,但我看到的那些变化多端且含糊不清.

And*_*owl 5

您不能在.cpp文件中放置类模板的成员函数定义.

在处理包含该调用的转换单元(即文件)时,编译器必须在调用这些函数时看到类模板的成员函数的定义.cpp.这允许编译器实际生成代码.除非调用这些函数,否则不会发生实例化.

现在将函数定义放在一个.cpp不包含这些函数调用的文件中,基本上是:

  1. 将它们密封到没有其他.cpp文件有机会看到它们的地方;
  2. 避免在唯一.cpp可以看到其定义的文件中对这些函数进行任何实例化.

因此,编译器不会为它们生成任何对象代码,并且链接器最终会抱怨找不到相应的符号(这就是你得到的错误).

要解决此问题,请将类模板的成员函数定义移动到类模板定义所在的相同标头中(AssArray.h在您的情况下),或者在#include调用这些函数的转换单元的标题中移动(并且调用之前).

  • 这是一个比目前标记的答案更好的答案. (3认同)

归档时间:

查看次数:

24286 次

最近记录:

12 年,6 月 前