使用类的静态函数模板成员的链接器错误
Linker errors using static function template members of a class
我不明白为什么它没有正确编译,链接器报告缺少符号:
建筑的未定义符号x86_64: "bool swinadventure::Utils::is_last<__gnu_cxx::__normal_iterator>>, std::vector>>(__gnu_cxx::__normal_iterator>>, std::vector> const&)",引用自: swinadventure::Inventory::get_item_list(std::string, std::string) in Inventory.cpp.o
因为在此过程中,我也看到了此错误:/usr/bin/ranlib: file: liblibswinadventure.a(Utils.cpp.o) has no symbols
我假设由于某种原因它没有正确编译 utils文件。
作为参考,我在Mac OS X 10.7系统上使用CMake,并安装和链接了大多数自制工具。
Utils.h
#ifndef UTILS_H_
#define UTILS_H_
#include <vector>
namespace swinadventure {
/**
* Static class for various utilities and helper functions
*/
class Utils {
public:
template <typename Iter>
static Iter next_iterator(Iter iter);
template <typename Iter, typename Cont>
static bool is_last(Iter iter, const Cont& cont);
};
} /* namespace swinadventure */
#endif /* UTILS_H_ */
实用工具.cpp
#include "Utils.h"
namespace swinadventure {
/**
* Returns the next iterator
* http://stackoverflow.com/questions/3516196/testing-whether-an-iterator-points-to-the-last-item
* @param iter
* @return
*/
template <typename Iter>
Iter Utils::next_iterator(Iter iter) {
return ++iter;
}
/**
* Checks if the iterator is the last of the vector array
* http://stackoverflow.com/questions/3516196/testing-whether-an-iterator-points-to-the-last-item
* @param iter iterator
* @param cont vector array
* @return
*/
template <typename Iter, typename Cont>
bool Utils::is_last(Iter iter, const Cont& cont)
{
return (iter != cont.end()) && (next_iterator(iter) == cont.end());
}
} /* namespace swinadventure */
将模板函数定义移动到标头。
链接器告诉您,在编译Inventory.cpp
时,需要Utils::is_last
的特殊实例化,但定义不可用于创建它。
在编译Utils.cpp
时,Utils::is_last
的定义是可用的,但不需要实例化。
因此,两个源文件在编译时都无法创建该函数。
声明模板时,<>内的变量将成为传递类型的替身,因此这些变量的使用方式与类相同,只需要声明一次。 因此template <typename Iter, typename Cont>
can 应被视为类定义,并移出class Utils {
定义。 template
的其他实例也应删除,尤其是在Utils::func()
定义之前。 就目前而言,你基本上是在告诉它"创建一个名为Iter的类。 创建一个名为 Iter 的类。 创建一个名为 Iter 的类,等等。一旦遇到类型为"Iter"的变量,它就不知道应该使用哪个变量。 这个网站应该对模板有帮助,值得一看:
http://www.parashift.com/c++-faq/templates.html
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误