C++ 模板方法的未定义引用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1111440/
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
Undefined reference error for template method
提问by recipriversexclusion
This has been driving me mad for the past hour and a half. I know it's a small thing but cannot find what's wrong (the fact that it's a rainy Friday afternoon, of course, does not help).
在过去的一个半小时里,这让我发疯了。我知道这是一件小事,但找不到问题所在(当然,这是一个下雨的星期五下午这一事实无济于事)。
I have defined the following class that will hold configuration parameters read from a file and will let me access them from my program:
我定义了以下类,它将保存从文件中读取的配置参数,并让我从我的程序中访问它们:
class VAConfig {
friend std::ostream& operator<<( std::ostream& lhs, const VAConfig& rhs);
private:
VAConfig();
static std::string configFilename;
static VAConfig* pConfigInstance;
static TiXmlDocument* pXmlDoc;
std::map<std::string, std::string> valueHash;
public:
static VAConfig* getInstance();
static void setConfigFileName( std::string& filename ) { configFilename = filename; }
virtual ~VAConfig();
void readParameterSet( std::string parameterGroupName );
template<typename T> T readParameter( const std::string parameterName );
template<typename T> T convert( const std::string& value );
};
where the method convert()
is defined in VAConfig.cpp
as
其中方法convert()
定义VAConfig.cpp
为
template <typename T>
T VAConfig::convert( const std::string& value )
{
T t;
std::istringstream iss( value, std::istringstream::in );
iss >> t;
return t;
}
All quite simple. But when I test from my main program using
一切都很简单。但是当我使用我的主程序进行测试时
int y = parameters->convert<int>("5");
I get an undefined reference to 'int VAConfig::convert<int>...'
compilation error. Ditto for readParameter()
.
我收到undefined reference to 'int VAConfig::convert<int>...'
编译错误。同上readParameter()
。
Looked at a lot of template tutorials but coul not figure this out. Any ideas?
看了很多模板教程,但无法弄清楚这一点。有任何想法吗?
回答by Seth Johnson
Templated code implementation should never be in a .cpp
file: your compiler has to see them at the same time as it sees the code that calls them (unless you use explicit instantiationto generate the templated object code, but even then .cpp
is the wrong file type to use).
模板化代码实现永远不应该在.cpp
文件中:您的编译器必须在看到调用它们的代码的同时看到它们(除非您使用显式实例化来生成模板化目标代码,但即便如此.cpp
也是错误的文件类型)用)。
What you need to do is move the implementation to either the header file, or to a file such as VAConfig.t.hpp
, and then #include "VAConfig.t.hpp"
whenever you use any templated member functions.
您需要做的是将实现移动到头文件或诸如VAConfig.t.hpp
, 之类的文件中,然后#include "VAConfig.t.hpp"
每当您使用任何模板化成员函数时。
回答by dma
If you move the implementation of the templated methods (convert and readParameter) to the header file, it should work.
如果您将模板化方法(convert 和 readParameter)的实现移动到头文件中,它应该可以工作。
The compiler must have access to the implementations of templated functions at the points where they're instantiated.
编译器必须能够在模板化函数的实例化点访问它们的实现。
回答by xtofl
A template method is merely a ... template for a method. The template arguments are to be filled in where the method is "instantiated".
模板方法仅仅是一个... 方法的模板。模板参数将在方法被“实例化”的地方填写。
It should be possible to build a compiler that is content with the declaration of a template method, and have a 'template compilation' step compile all the needed instances of the template method.
应该可以构建一个满足模板方法声明的编译器,并有一个“模板编译”步骤来编译模板方法的所有需要的实例。
This is not the case for microsoft's vc. I heard a collegue mutter about it being the case on unix, though.
微软的vc就不是这种情况。不过,我听到一位同事嘟囔着说在 Unix 上就是这种情况。
Most compilers instantiate the template method on request, where they are used in the source code. In order to instantiate the method, the compiler must 'see' the template function body. That's why the body is most often placed in either the header file, or in e.g. a .h.cpp file, which is then included as the last line of the .h file.
大多数编译器根据请求实例化模板方法,在源代码中使用它们。为了实例化该方法,编译器必须“查看”模板函数体。这就是为什么正文最常放在头文件中,或者放在例如 .h.cpp 文件中,然后将其作为 .h 文件的最后一行包含在内。