我有一个带有 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 是一个 Enum,但这与问题无关
//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();
所有三个 init 函数都已经过测试并且可以正常工作,它们的定义方式完全相同并且都是静态的
出于某种原因,我无法取消注释 .cpp 中的名称。如果这样做,我会收到以下错误:
error: conflicting declaration 'const QMap<QString, Customer::Type> Customer::names'
我试过重命名,把它移到别处,总是这个不起作用,我不知道为什么?
但是其他的没问题..
最佳答案
在您的 cpp 文件中,模板参数的顺序错误:
QMap<QString, Customer::Type> const Customer::names = Customer::initNames();
应该是:
QMap<Customer::Type, QString> const Customer::names = Customer::initNames();
或者头文件中的变量声明应该根据 Customer::initNames()
的返回类型进行更改
关于c++ - 静态 QMap 的冲突声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33348981/