使用类的静态函数模板成员的链接器错误

Linker errors using static function template members of a class

本文关键字:链接 错误 成员 静态 函数模板      更新时间:2023-10-16

我不明白为什么它没有正确编译,链接器报告缺少符号:

建筑的未定义符号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