为什么这个模板示例代码没有编译

Why is this template example code not compiling?

本文关键字:代码 编译 为什么      更新时间:2023-10-16

为了学习如何使用模板,我正在尝试制作一个使用模板的示例。我在Visual Studio 2010 C++中用三个文件做了一个项目:

Controller.cpp:

#include <iostream>
#include "Foo.h"
using namespace std;
int main(){
    Example<int,string> example;
    example.appols(5, "Hi");
    return 0;
}

Foo.h:

#include <iostream> 
using namespace std;
template <class T, class E>
class Foo{
public:
    void printThis(T t, E e);
};
template <class T, class E>
class Example{
public:
    void appols(T t, E e);
};

Foo.ipp:

#include <iostream>
#include "Foo.h"
using namespace std;
template<class T, class E>
void Foo<T, E>::printThis(T t, E e){
    cout << "FOO! " << t << ' ' << e << endl;
}
template<class T, class E>
void Example<T, E>::appols(T t, E e){
    cout << "APPOLS!! " << t << ' ' << e << endl;
    Foo<T,E> f;
    f.printThis(t,e);
}

当我尝试编译时,我得到:

1>------ Build started: Project: Template Practive, Configuration: Debug Win32 ------
1>Controller.obj : error LNK2019: unresolved external symbol "public: void __thiscall Example<int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::appols(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?appols@?$Example@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>C:UsersMatthewdocumentsvisual studio 2010ProjectsTemplate PractiveDebugTemplate Practive.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

你能帮我看看我在这里做错了什么吗?error LNK2019是从哪里来的?提前谢谢!

解密错误消息

让我们先尝试解码错误消息:

1>Controller.obj : error LNK2019: unresolved external symbol 
"public: void __thiscall Example<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::appols(int,class
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"
(?appols@?$Example@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 
referenced in function _main

我们可以在这个错误消息中开始降低信噪比。

(?appols@?$Example@HV?$basic_string@DU?$char_traits@D@std@@...的部分只是C++名称篡改。它是为C++编译器设计的,并不是为了让人可读。所以我们可以丢弃它。

简化的错误消息变为:

1>Controller.obj : error LNK2019: unresolved external symbol 
"public: void __thiscall Example<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::appols(int,class
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"
referenced in function _main

第一行告诉我们存在一个"未解析的外部符号"。让我们试着识别它,看看下一行。

零件:

class std::basic_string<char,struct std::char_traits<char>...

只是std::string的长名称(对应于以char为字符类型并使用默认char特征和默认分配器的std::basic_string模板)。

因此,我们可以用std::string代替那些冗长的部分来进一步降低噪声:

1>Controller.obj : error LNK2019: unresolved external symbol 
"public: void __thiscall Example<int,string>::appols(int,string)"
referenced in function _main

现在这个错误更清楚了。

__thiscall部分只是默认情况下用于C++成员函数的调用约定。

因此,链接器正在抱怨这种方法:

"public: void Example<int,string>::appols(int,string)"

对应于您的Example<T,E>::appols():

template <class T, class E>
class Example{
public:
    void appols(T t, E e);
};

当CCD_ 10和CCD_。

这与Controller.cpp中main()函数中的内容相对应:

Example<int,string> example;
example.appols(5, "Hi");

问题的诊断和解决方案

问题是,所有模板源代码必须在头文件中,而不是.cpp文件中。

事实上,编译器需要模板代码的定义来实例化它

因此,您可以将代码从Foo.ipp文件移动到Foo.h(或仅从Foo.h移动#include "Foo.ipp"),并将inline添加到模板函数定义中:

// New code in Foo.h
template<class T, class E>
inline void Foo<T, E>::printThis(T t, E e){
    cout << "FOO! " << t << ' ' << e << endl;
}
template<class T, class E>
inline void Example<T, E>::appols(T t, E e){
    cout << "APPOLS!! " << t << ' ' << e << endl;
    Foo<T,E> f;
    f.printThis(t,e);
}

还要注意的是,using namespace std;在头文件中非常糟糕,因为这会污染所有包含头的客户端的全局命名空间。只需在头文件中明确使用std::前缀(例如:std::coutstd::endl等)。