c++ - 静态 QMap 的冲突声明

标签 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 是一个 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/

相关文章:

c++ - 解决难题(最佳解决方案)

c - 此 netbsd 代码是否可重入

c++单例,静态变量分配有new

java - 为什么我收到错误 "non-static variable this cannot be referenced from a static context"?

C++ 优先队列实现对图边进行排序

java - C++ 与 Java 构造函数

c++ - 如何用模板解决这个循环继承问题?

linux - Linux源码编译问题

c++ - 如何在 Qt(gcc 64 位)中禁用自动初始化局部变量

c++ - Qt中的全局变量,如何?