我以前做过明确的专业化,我只是不明白为什么这不起作用:
StringUtils.hpp
#ifndef AYC_STRINGUTILS_HPP
#define AYC_STRINGUTILS_HPP
#include <string>
class StringUtils {
public:
template <typename T>
static std::string toString(const T& t);
};
#include "StringUtils.tpp"
#endif //AYC_STRINGUTILS_HPP
StringUtils.tpp
#include "StringUtils.hpp"
template<typename T>
std::string StringUtils::toString(const T& t) {
return std::to_string(t);
}
template<>
std::string StringUtils::toString<std::string>(const std::string& t) {
return t;
}
我得到的错误是抱怨函数的多个定义的链接器错误toString
。
项目中的许多文件使用#include "StringUtils.hpp"
.
如何尝试修复此错误?课堂上有什么问题StringUtils
吗?