静态QMap的冲突声明

时间:2015-10-26 15:01:24

标签: c++ qt static qmap

我有一个带有3个静态QMap的简单Customer类

//customer.h

class Customer : public QObject
{
    Q_OBJECT
public:
    static QMap<Customer::Type, QString> const names;
    static QMap<QString, Customer::Type> const keywords;
    static QMap<Customer::Type, QString> const debugStrings;
};

Customer :: Type是一个枚举,但这与问题无关

//customer.cpp

//QMap<QString, Customer::Type> const Customer::names = Customer::initNames();
QMap<QString, Customer::Type> const Customer::keywords = Customer::initKeywords();
QMap<Customer::Type, QString> const Customer::debugStrings = Customer::initDebugStrings();

所有三个初始化函数都经过测试并且工作得很好,它们的定义方式完全一样,都是静态的

出于某种原因,我无法取消注释.cpp中的名称。如果我这样做,我会收到以下错误:

error: conflicting declaration 'const QMap<QString, Customer::Type> Customer::names'

我尝试重命名,将其移动到其他地方,它始终是不起作用的,我不知道为什么?

但是其他的没有问题..

1 个答案:

答案 0 :(得分:6)

在cpp文件中,模板参数的顺序错误:

QMap<QString, Customer::Type> const Customer::names = Customer::initNames();

应该是:

QMap<Customer::Type, QString> const Customer::names = Customer::initNames();

或者,应根据Customer::initNames()

的返回类型更改头文件中的变量声明