简体   繁体   English

如何定义模板类并将其划分为多个文件?

[英]How do I define a template class and divide it into multiple files?

I have written a simple template class for test purpose. 我编写了一个简单的模板类用于测试目的。 It compiles without any errors, but when I try to use it in main(), it give some linker errors. 它编译没有任何错误,但是当我尝试在main()中使用它时,它会给出一些链接器错误。


main.cpp main.cpp中

#include <iostream>
#include "MyNumber.h"

int wmain(int argc, wchar_t * argv[])
{
    MyNumber<float> num;
    num.SetValue(3.14);
    std::cout << "My number is " << num.GetValue() << "." << std::endl;

    system("pause");
    return 0;
}



MyNumber.h MyNumber.h

#pragma once

template <class T>
class MyNumber
{
    public:
        MyNumber();
        ~MyNumber();
        void SetValue(T val);
        T GetValue();

    private:
        T m_Number;
};



MyNumber.cpp MyNumber.cpp

#include "MyNumber.h"

template <class T>
MyNumber<T>::MyNumber()
{
    m_Number = static_cast<T>(0);
}

template <class T>
MyNumber<T>::~MyNumber()
{
}

template <class T>
void MyNumber<T>::SetValue(T val)
{
    m_Number = val;
}

template <class T>
T MyNumber<T>::GetValue()
{
    return m_Number;
}



When I build this code, I get the following linker errors: 当我构建此代码时,我收到以下链接器错误:

Error 7 Console Demo C:\\Development\\IDE\\Visual Studio 2010\\SAVE\\Grand Solution\\X64\\Debug\\Console Demo.exe 1 error LNK1120: 4 unresolved externals 错误7控制台演示C:\\ Development \\ IDE \\ Visual Studio 2010 \\ SAVE \\ Grand Solution \\ X64 \\ Debug \\ Console Demo.exe 1错误LNK1120:4个未解析的外部

Error 3 Console Demo C:\\Development\\IDE\\Visual Studio 2010\\SAVE\\Grand Solution\\Console Demo\\main.obj error LNK2019: unresolved external symbol "public: __cdecl MyNumber::~MyNumber(void)" (??1?$MyNumber@M@@QEAA@XZ) referenced in function wmain 错误3控制台演示C:\\ Development \\ IDE \\ Visual Studio 2010 \\ SAVE \\ Grand Solution \\ Console Demo \\ main.obj错误LNK2019:未解析的外部符号“public:__ cdecl MyNumber ::〜MyNumber(void)”(?? 1? $ MyNumber @ M @@ QEAA @ XZ)在函数wmain中引用

Error 6 Console Demo C:\\Development\\IDE\\Visual Studio 2010\\SAVE\\Grand Solution\\Console Demo\\main.obj error LNK2019: unresolved external symbol "public: __cdecl MyNumber::MyNumber(void)" (??0?$MyNumber@M@@QEAA@XZ) referenced in function wmain 错误6控制台演示C:\\ Development \\ IDE \\ Visual Studio 2010 \\ SAVE \\ Grand Solution \\ Console Demo \\ main.obj错误LNK2019:未解析的外部符号“public:__ cdecl MyNumber :: MyNumber(void)”(?? 0?$ MyNumber @ M @@ QEAA @ XZ)在函数wmain中引用

Error 4 Console Demo C:\\Development\\IDE\\Visual Studio 2010\\SAVE\\Grand Solution\\Console Demo\\main.obj error LNK2019: unresolved external symbol "public: float __cdecl MyNumber::GetValue(void)" (?GetValue@?$MyNumber@M@@QEAAMXZ) referenced in function wmain 错误4控制台演示C:\\ Development \\ IDE \\ Visual Studio 2010 \\ SAVE \\ Grand Solution \\ Console Demo \\ main.obj错误LNK2019:未解析的外部符号“public:float __cdecl MyNumber :: GetValue(void)”(?GetValue @? $ MyNumber @ M @@ QEAAMXZ)在函数wmain中引用

Error 5 Console Demo C:\\Development\\IDE\\Visual Studio 2010\\SAVE\\Grand Solution\\Console Demo\\main.obj error LNK2019: unresolved external symbol "public: void __cdecl MyNumber::SetValue(float)" (?SetValue@?$MyNumber@M@@QEAAXM@Z) referenced in function wmain 错误5控制台演示C:\\ Development \\ IDE \\ Visual Studio 2010 \\ SAVE \\ Grand Solution \\ Console Demo \\ main.obj错误LNK2019:未解析的外部符号“public:void __cdecl MyNumber :: SetValue(float)”(?SetValue @? $ MyNumber @ M @@ QEAAXM @ Z)在函数wmain中引用

But, if I leave main() empty, I don't get any linker errors. 但是,如果我将main()留空,我不会收到任何链接器错误。

What is wrong with my template class? 我的模板类有什么问题?
What am I doing wrong? 我究竟做错了什么?

You have to instantiate your template explicitly for each template parameter you use. 您必须为您使用的每个模板参数显式实例化模板。

Ie, add the following line at the end of the MyNumber.cpp file: 即,在MyNumber.cpp文件的末尾添加以下行:

template class MyNumber<float>;

This way, the linker will be able to find all template instantiations it needs. 这样,链接器将能够找到它需要的所有模板实例化。

See also Moving Templates Out of Header Files . 另请参阅从标头文件移出模板

You can not implement the template in a cpp file. 您无法在cpp文件中实现该模板。 You need to define the class methods in the header file itself. 您需要在头文件本身中定义类方法。 See this Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file? 请参阅此为什么我不能将模板类的定义与其声明分开并将其放在.cpp文件中? for more information. 欲获得更多信息。

Well, you're actually supposed to be able to use the export keyword, but almost no compilers implement it. 好吧,你实际上应该能够使用export关键字,但几乎没有编译器实现它。 You can work around this by extracting non-generic code into separate functions, and define these in separate files. 您可以通过将非通用代码提取到单独的函数中来解决此问题,并在单独的文件中定义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM