错误 LNK2019:未解析的外部符号,C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14843006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR LNK2019:unresolved external symbol, c++
提问by Nattfrosten
I have written a template class, for a map/dictionary data structure, and keep getting this strange error (ERROR LNK2019:unresolved external symbol)
我已经为地图/字典数据结构编写了一个模板类,并且不断收到这个奇怪的错误(ERROR LNK2019:unresolved external symbol)
Code:
代码:
AssArray.h:
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);
};
AssArray.cpp:
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
}
}
The errors are:
1
错误是:
1
"Error 4 error LNK1120: 3 unresolved externals
C:\Users\*****\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\Debug
\OOPLAB4NYARE.exe 1 1 OOPLAB4NYARE"
2
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
3
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
4
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
I am using Microsoft visual studio 2010 Ultimate. It's most likely something easy I've just been overlooking.
我正在使用 Microsoft Visual Studio 2010 Ultimate。这很可能是我刚刚忽略的一些简单的事情。
I have tried to clean->rebuild, created a new project and copy pasted the relevant code, as well as trying to find a solution, but the ones I've seen are pretty varied and vague.
我尝试清理-> 重建,创建一个新项目并复制粘贴相关代码,并尝试找到解决方案,但我所看到的解决方案非常多样且含糊不清。
回答by Andy Prowl
You cannot put member function definitions for a class template in .cpp
files.
您不能将类模板的成员函数定义放在.cpp
文件中。
The definitions of member functions of a class template must be seen by the compiler at the point those functions are invoked, while processing the translation unit (i.e. .cpp
file) that contains that invocation. This allows the compiler to actually generate the code. No instantiation occurs unless those functions are invoked.
类模板的成员函数的定义必须在这些函数被调用时被编译器看到,同时处理.cpp
包含该调用的翻译单元(即文件)。这允许编译器实际生成代码。除非调用这些函数,否则不会发生实例化。
Now by putting your function definitions in a .cpp
file which contains no invocation of those functions, you are basically:
现在,通过将您的函数定义放在一个.cpp
不包含这些函数调用的文件中,您基本上是:
- sealing them into a place where no other
.cpp
file has a chance to see them; - avoiding any instantiation of those functions in the only
.cpp
file that can see their definitions.
- 将它们密封在其他
.cpp
文件无法看到的地方; - 避免在
.cpp
可以看到其定义的唯一文件中对这些函数进行任何实例化。
Therefore, the compiler will not generate any object code for them, and the linker will eventually complain that the corresponding symbols are not found (that's the error you get).
因此,编译器不会为它们生成任何目标代码,链接器最终会抱怨找不到相应的符号(这就是您得到的错误)。
To solve the problem, move the member function definitions of your class template into the same header where the class template definition is (AssArray.h
in your case), or in a header which is #include
d by the translation unit(s) where those functions are invoked (and beforethe points of invocation).
要解决此问题,请将类模板的成员函数定义移动到类模板定义所在的同一标头中(AssArray.h
在您的情况下),或移动到#include
调用这些函数的翻译单元所包含的标头中(和之前调用的点)。
回答by user1610015
Methods of template classes need to be implemented in the same header that defines the class (usually they are implemented inside the class itself, as inline methods).
模板类的方法需要在定义类的同一个头文件中实现(通常它们在类本身内部实现,作为内联方法)。