【问题标题】:conflicting declaration of static QMap静态 QMap 的声明冲突
【发布时间】:2015-10-26 15:01:23
【问题描述】:

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

//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();

所有三个初始化函数都已经过测试并且工作正常,它们的定义方式完全相同并且都是静态的

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

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

我试过重命名,把它移到别的地方,总是这个不起作用,我不知道为什么?

但其他的工作没有问题..

【问题讨论】:

  • 你有同名的类方法吗?无论如何,你应该提供完整的类声明。

标签: c++ qt static qmap


【解决方案1】:

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

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

应该是:

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

或者你的头文件中的变量声明应该根据Customer::initNames()的返回类型改变

【讨论】:

  • 我现在感觉真的很蠢 :-) 非常感谢,我已经为此昏迷了将近一个小时
  • 这种错误很容易漏掉:)
  • 经典复制粘贴错误:)
猜你喜欢
  • 1970-01-01
  • 2017-07-20
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 1970-01-01
相关资源
最近更新 更多