当前位置:网站首页>Qt compile error: C2228: '.key' must have class/struct/union on the left
Qt compile error: C2228: '.key' must have class/struct/union on the left
2022-07-31 09:48:00 【Jason~shen】
目录
方案1:Static functions access static variables
方案2:Objects are defined inside static functions,Access the variable again
一、产生原因
原因:A static function accesses a non-static variable, staticfunction and notstatic变量混用,示例代码如下
class FLOWEDITORSHARED_EXPORT NETranslator
{
public:
NETranslator();
static QString ch2En(const QString &ch);
static QString en2Ch(const QString &en);
private:
QHash<QString, QString> m_hash;
};
//
NETranslator::NETranslator()
{
m_hash.insert("image", QStringLiteral("图像数据"));
m_hash.insert("region", QStringLiteral("区域数据"));
m_hash.insert("contours", QStringLiteral("轮廓数据"));
}
QString NETranslator::ch2En(const QString &ch)
{
QString en = m_hash.key(ch, ch);
return en;
}
QString NETranslator::en2Ch(const QString &en)
{
QString ch = m_hash.value(en, en);
return ch;
}
Compilation of the above code will give the following error:
NETranslator.cpp:12: error: C2228: “.key”的左边必须有类/结构/联合
NETranslator.cpp:18: error: C2228: “.value”的左边必须有类/结构/联合
二、解决方案
方案1:Static functions access static variables
Then declare the class variable as staticIs it useful,修改代码如下:
class FLOWEDITORSHARED_EXPORT NETranslator
{
public:
NETranslator();
static QString ch2En(const QString &ch);
static QString en2Ch(const QString &en);
private:
static QHash<QString, QString> m_hash; //修改了此处,静态声明
};
Compiling again gave the following error
NETranslator.obj error: LNK2019: 无法解析的外部符号 "public: static class QHash<class QString,class QString> NETranslator::m_hash" ([email protected]@@[email protected]@@[email protected]@@A),该符号在函数 "public: __cdecl NETranslator::NETranslator(void)" ([email protected]@[email protected]) 中被引用
Unresolved external symbols are mostly just declared,没有定义.Static members of a class must be declared within the class,在类外初始化.修改代码如下:
class FLOWEDITORSHARED_EXPORT NETranslator
{
public:
NETranslator();
static QString ch2En(const QString &ch);
static QString en2Ch(const QString &en);
private:
static QHash<QString, QString> m_hash; //修改了此处,静态声明
};
//
QHash<QString, QString> NETranslator::m_hash; //修改了此处,定义
NETranslator::NETranslator()
{
m_hash.insert("image", QStringLiteral("图像数据"));
m_hash.insert("region", QStringLiteral("区域数据"));
m_hash.insert("contours", QStringLiteral("轮廓数据"));
}
QString NETranslator::ch2En(const QString &ch)
{
QString en = m_hash.key(ch, ch);
return en;
}
QString NETranslator::en2Ch(const QString &en)
{
QString ch = m_hash.value(en, en);
return ch;
}
There is also a statement,There are also definitions,编译通过.上述方案m_hashAlthough the value is inserted,But the function is accessedm_hash数据为空.
原因:使用NETranslator::ch2En() 方式去调用,并没有调用NETranslator构造函数,So there is no initialization data
那怎么办了,It can only be assigned when a static variable is initialized,修改代码如下:
QHash<QString, QString> LinkTranslator::m_hash =
{
{"image", QStringLiteral("图像数据")},
{"region", QStringLiteral("区域数据")},
{"contours", QStringLiteral("轮廓数据")},
};
方案2:Objects are defined inside static functions,Access the variable again
If you initialize the data in the constructor,Just call the constructor and that's it
class FLOWEDITORSHARED_EXPORT NETranslator
{
public:
NETranslator();
static QString ch2En(const QString &ch);
static QString en2Ch(const QString &en);
private:
QHash<QString, QString> m_hash;
};
NETranslator::NETranslator()
{
m_hash.insert("image", QStringLiteral("图像数据"));
m_hash.insert("region", QStringLiteral("区域数据"));
m_hash.insert("contours", QStringLiteral("轮廓数据"));
}
QString NETranslator::ch2En(const QString &ch)
{
NETranslator tran; //修改了此处
QString en = tran.m_hash.key(ch, ch);
return en;
}
QString NETranslator::en2Ch(const QString &en)
{
NETranslator tran; //修改了此处
QString ch = tran.m_hash.value(en, en);
return ch;
}
边栏推荐
猜你喜欢
随机推荐
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
【软考软件评测师】2012综合知识历年真题
JSP page对象简介说明
js滚动条滚动到指定元素
(C语言)程序环境和预处理
生成随机数
【NLP】Transformer理论解读
Come n times with the sword--05. Replace spaces
Redis集群-哨兵模式原理(Sentinel)
JS中原型和原型链的详细讲解(附代码示例)以及 new关键字具体做了什么的详细讲解
qt pass custom structure parameters in different threads
MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题)
安装gnome-screenshot截图工具
ReentrantLock
乐观锁和悲观锁
Flink1.15源码阅读flink-clients——flink命令行帮助命令
第六章
NowCoderTOP17-22 二分查找/排序——持续更新ing
7. JS ES6新增语法 new Map详讲,还有一道代码实战案例帮你快上手new Map
MySQL 高级(进阶) SQL 语句 (一)