声明和定义不匹配
Declaration and definition mismatch
我的模板中的声明和定义不匹配class(所有不相关的内容都已删除):
template <typename LanguageMap>
class WidgetLanguageManager
{
public:
enum class Language { DE, ENG, PL };
protected:
Language language;
//Some protected fields
public:
explicit WidgetLanguageManager(const Language& language);
explicit WidgetLanguageManager(const QString& language);
Language ToLanguage(const QString& language);
protected:
//some protected methods
};
template <typename LanguageMap>
Language WidgetLanguageManager<LanguageMap>::ToLanguage(const QString& language) // this cannot be matched with a declaration
{
for (QChar& c: language)
{
c = c.toLower();
}
switch (language)
{
case "german": return Language::DE;
case "english": return Language::ENG;
case "polish": return Language::PL;
}
throw RuntimeError("Wrong language paremeter.");
}
请帮忙,因为我看不出背后有任何原因。
以下方法可行:
template <typename LanguageMap>
typename WidgetLanguageManager<LanguageMap>::Language WidgetLanguageManager<LanguageMap>::ToLanguage(const QString& language)
// ^^^^^ 'typename' keyword needed because 'Language' is dependent name
由于 enum class Language
是 WidgetLanguageManager
模板 class 的一部分,您需要在它前面加上范围运算符。
除了额外的 typename
,您可以使用尾随 return 类型:
template <typename LanguageMap>
auto WidgetLanguageManager<LanguageMap>::ToLanguage(const QString& language)
-> Language
{
/*..*/
}
我的模板中的声明和定义不匹配class(所有不相关的内容都已删除):
template <typename LanguageMap>
class WidgetLanguageManager
{
public:
enum class Language { DE, ENG, PL };
protected:
Language language;
//Some protected fields
public:
explicit WidgetLanguageManager(const Language& language);
explicit WidgetLanguageManager(const QString& language);
Language ToLanguage(const QString& language);
protected:
//some protected methods
};
template <typename LanguageMap>
Language WidgetLanguageManager<LanguageMap>::ToLanguage(const QString& language) // this cannot be matched with a declaration
{
for (QChar& c: language)
{
c = c.toLower();
}
switch (language)
{
case "german": return Language::DE;
case "english": return Language::ENG;
case "polish": return Language::PL;
}
throw RuntimeError("Wrong language paremeter.");
}
请帮忙,因为我看不出背后有任何原因。
以下方法可行:
template <typename LanguageMap>
typename WidgetLanguageManager<LanguageMap>::Language WidgetLanguageManager<LanguageMap>::ToLanguage(const QString& language)
// ^^^^^ 'typename' keyword needed because 'Language' is dependent name
由于 enum class Language
是 WidgetLanguageManager
模板 class 的一部分,您需要在它前面加上范围运算符。
除了额外的 typename
,您可以使用尾随 return 类型:
template <typename LanguageMap>
auto WidgetLanguageManager<LanguageMap>::ToLanguage(const QString& language)
-> Language
{
/*..*/
}