警告C4661:没有为显式模板实例化请求提供适当的定义 [英] warning C4661:no suitable definition provided for explicit template instantiation request

查看:134
本文介绍了警告C4661:没有为显式模板实例化请求提供适当的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个类模板,并在不同的DLL中使用它,因此希望隐藏实现的某些部分。

I wrote a class template and use it in different DLLs, so wish to hide some parts of the implementation.

为此,我使用模板实例化 ,但是像这样导出它,这是头文件:

To do this, I use "template instantiation", but export it, like this, here is the header file:

#include <iostream>
#include <exception>

using namespace std;

template<typename T>
class __declspec(dllexport) Templated
{
    public:
        Templated();
};

template __declspec(dllexport) Templated<int>;

int main()
{
   cout << "Hello World" << endl; 
}

并且定义在单独的文件(.cpp)中

And the definition is in a separate file (.cpp)

template<typename T>
Templated<T>::Templated() {}

template Templated<int>;

我的问题是,即使实例化被标记为已导出,我也得到了警告!

My problem is that I got a warning, even if the instantiation is marked as exported!

您可以在此处测试此代码: http://webcompiler.cloudapp。 net / ,它将生成C4661警告!

You can test this code here : http://webcompiler.cloudapp.net/, it will generate the C4661 warning!

这正常吗?

推荐答案

您声明模板的显式实例。很好,但是您无法为构造函数提供定义:您只能声明,而模板没有定义,也没有实例化。

You declare an explicit instantiation of your template. That's fine, but you fail to provide a definition for the constructor: you only declare it, and there is no definition for the template nor it's instantiation.

您必须在模板本身中提供一个定义:

You must provide a definition either in the template itself:

...
public:
    Templated() {};  // empty but defined ctor
...

或用于专业化:

Templated<int>::Templated() {
}






根据您的注释并编辑至问题,定义位于另一个cpp文件中。问题是对于编译器来说,每个cpp文件都是一个不同的翻译单元。换句话说,当第一个文件被编译时,编译器不知道另一个文件。这就是为什么您得到警告而不是错误的原因:警告的意思是嘿,程序员,您向我声明您想要模板化的专用实例化,但是我找不到它的构造函数。希望您已经在另一个翻译单元中对其进行了定义,因为如果您没有定义它,那么在链接时会收到错误消息。正如您实际上已在另一个文件中定义它一样,您可以放心地忽略该警告。


From your comment and edit to question, the definition is in another cpp file. The problem is that for the compiler, each cpp file is a different translation unit. Said differently, when the first file is compiled, the compiler does not know the other one. That's the reason why you get a warning and not an error: the warning means hey programmer, you declare me that you want a specialized instantiation of Templated, but I cannot find its constructor. Hope you have defined it in another translation unit, because if you have not you'll get an error at link time. As you have actually defined it in another file, you can safely ignore the warning.

警告仅表示发生了不常见。通常,期望显式专业化的声明与其所有必需的定义都在同一翻译单元中。而且恕我直言,您应该坚持使用该用法以避免警告,更重要的是,要拥有一个更具维护性的应用程序。

A warning only says that something uncommon is occuring. Normally, it is expected for the declaration of an explicit specialization to be in the same translation unit as all its required definitions. And IMHO, you should stick to that usage to avoid the warning and, more importantly, to have a more maintainable application.

这篇关于警告C4661:没有为显式模板实例化请求提供适当的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆