我正在尝试使用本文中描述的方法修复“非命名空间范围内的显式专业化”错误。所以我设法将模板化函数移动到一个单独的命名空间中,并从我的类中调用这些函数(参见下面的代码)。
代码现在可以编译,但是对于命名空间中的所有函数,我收到“已经在 main.obj 中定义”的链接器错误。我认为#ifndef STYLE_H
在顶部添加会阻止命名空间被多次包含,或者我错过了什么?我该如何解决这个错误?
以下是我的代码(简化):
#ifndef STYLE_H
#define STYLE_H
namespace hanzi {
namespace styleUtil {
template <class T>
T get(const QString& name, T defaultValue = T(), const Style* style = NULL, const Style* baseStyle = NULL) {
// ...
}
template <>
QColor get<QColor>(const QString& name, QColor defaultValue, const Style* style, const Style* baseStyle) {
// ...
}
template <>
ImagingEffect get<ImagingEffect>(const QString& name, ImagingEffect defaultValue, const Style* style, const Style* baseStyle) {
// ...
}
}
class Style : public QObject {
Q_OBJECT
public:
explicit Style(const QString& filePath);
template <class T>
T get(const QString& name, T defaultValue = T()) const {
return styleUtil::get<T>(name, defaultValue, this, baseStyle_);
};
};
}
#endif // STYLE_H