我需要帮助修复错误消息 - “错误LNK2019:未解决的外部符号” [英] I need help fixing an error message - "error LNK2019: unresolved external symbol"
问题描述
我尝试编译时出现此错误消息
I am having this error message whenever I tried to compile
Error 6 error LNK2019: unresolved external symbol "void __cdecl MergeSort(class LinkedList<int> &)" (?MergeSort@@YAXAAV?$LinkedList@H@@@Z) referenced in function _main C:\Users\FOla Yinka\Documents\Visual Studio 2012\Projects\C++\C++\linkedlist.obj C++
Error 7 error LNK1120: 1 unresolved externals C:\Users\FOla Yinka\Documents\Visual Studio 2012\Projects\C++\Debug\C++.exe 1 1 C++
我的头文件中有这个
template<typename T>
class LinkedList{
protected:
public:
friend void MergeSort(LinkedList<T> &list);
};
template<typename T>
void MergeSort(LinkedList<T> &list){
}
$ b b
要检查错误是否在函数声明中,我将所有 protected
成员 public
删除友谊所以 MergeSort
可以访问所有memebers,然后程序编译成功。我不知道为什么会出现此错误消息。
To check if the fault is in the function declaration, I turned all the protected
members public
and I removed the friendship so MergeSort
can have access to all memebers and then the program compiled successfully. I don't know why I am having this error message.
推荐答案
一个可能的解决方案是定义 friend
:
A possible solution is to define the friend
within the class body:
template<typename T>
class LinkedList{
protected:
public:
friend void MergeSort(LinkedList<U> &list)
{}
};
另一个解决方案是声明 friend
在类主体之前,以便它知道朋友
是模板
:
The other solution is to declare the friend
before the class body so that it knows the friend
is a template
:
template <typename T> class LinkedList ;
template <typename T> void MergeSort(LinkedList<T> &list) ;
,然后在类体中声明 friend
like this:
and then in the class body declare the friend
like this:
friend void MergeSort<>(LinkedList<T> &list) ;
此 C ++常见问题条目介绍了为什么在这种情况下需要特殊解决方法的完整详细信息。
This C++ FAQ entry goes into the complete details of why you need a special work-around in this case.
这篇关于我需要帮助修复错误消息 - “错误LNK2019:未解决的外部符号”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!