开发环境: Qt Creator 4.8.2

在写程序的时候,遇到了编译器报错

error: must use ‘class’ tag to refer to type ‘XXX’ in this scope

出错的代码部分如下:

Iterator* ConcreteAggregate::ConcreteIteratorDesc()
{
return new ConcreteIteratorDesc(this); //error: must use 'class' tag to refer to type 'ConcreteIteratorDesc' in this scope
}

根据提示,修改代码如下后,编译器错误消失

Iterator* ConcreteAggregate::ConcreteIteratorDesc()
{
// return new ConcreteIteratorDesc(this);
return new class ConcreteIteratorDesc(this);
}

经过排查,终于发现了这个错误的原因,首先定义了一个抽象类及其子类

class Aggregate
{
public:
virtual Iterator* CreateIterator() = 0;
virtual Iterator* ConcreteIteratorDesc() = 0;
virtual vector<object>* GetVector() = 0;
};
//定义一个具体的聚合类
class ConcreteAggregate : public Aggregate
{
public:
ConcreteAggregate();
~ConcreteAggregate();
Iterator* CreateIterator() override;
Iterator* ConcreteIteratorDesc() override;
int GetCount() const;
vector<object>* GetVector() override;
object GetElement(int index);
private:
vector<object>* items;
};

然后又定义了一个类

//实现从前向后的迭代器
class ConcreteIterator : public Iterator
{
public:
//初始化时将具体的聚合对象传入
ConcreteIterator(Aggregate* aggregate);
object First() override;
object Next() override;
object CurrentItem() override;
bool IsDone() override;
private:
ConcreteAggregate* m_aggregate; //定义了一个具体聚集对象
int current;
};

注意看可以发现,新定义的类名为ConcreteIterator,而Aggregate类中有同名的函数

   virtual Iterator* ConcreteIteratorDesc() = 0;

在实现子类ConcreteAggregate的ConcreteIteratorDesc()函数时,编译器报错

Iterator* ConcreteAggregate::ConcreteIteratorDesc()
{
return new ConcreteIteratorDesc(this);
// return new class ConcreteIteratorDesc(this);
}

解决方案:

  • 一种解决办法是在类前面加class,告诉编译器,调用关键字的位置是一个类
Iterator* ConcreteAggregate::ConcreteIteratorDesc()
{
return new class ConcreteIteratorDesc(this);
}
  • 另一种解决办法就是更换关键字

参考资料:

1 https://blog.csdn.net/fjjaylz/article/details/88744885 关于C++中class关键字的一种特别使用情况

error: must use ‘class’ tag to refer to type ‘XXX’ in this scope的更多相关文章

  1. ERROR 1045 (28000): Access denied for user xxx & ERROR 1449 (HY000): The user specified as a definer xxx does not exists

    今天在一个修改过权限的MySQL数据库遇到了"ERROR 1045 (28000): Access denied for user 'xxx'@'xxx.xxx.xxx.xxx' (usin ...

  2. ERROR 1526 (HY000): Table has no partition for value xxx

    最近,我们有些功能需要使用到基于多个字段的分区,需要同时支持oracle/mysql,但是开发人员又希望尽可能少的改动业务代码,也不愿意使用多列分区,在oracle 11g之前,不支持多列分区(12. ...

  3. Remote error: VAR and OUT arguments must match parameter type exactly'

    在XE10中 downloadfile(filename: string; out FileStream: TStream; out FileSize: int64)是没有问题的,升级到delphi ...

  4. 报错:OpenCV Error: Assertion failed (src.size() == dst.size() && src.type() == dst.ty pe()) in unknown function, file ..……

    在用cvDilate函数的时候,老是导致程序中断,报错如下: OpenCV Error: Assertion failed (src.size() == dst.size() && s ...

  5. linux下mysql启动 Starting MySQL. ERROR! The server quit without updating PID file(xxx/x.pid)

    service mysql start 报错: Starting MySQL. ERROR! The server quit without updating PID file(xxx/x.pid) ...

  6. MySQL数据库插入数据出现 ERROR 1526 (HY000): Table has no partition for value xxx

    MySQL数据库插入数据出现ERROR 1526 (HY000): Table has no partition for value xxx工作的时候发现无法插入数据,报错:ERROR 1526 (H ...

  7. org.apache.xmlbeans.XmlException: error: does not close tag

    使用myeclipse的jax自动生成webservice , 或者serviceImpl通过@webservice来实现webservice时, 使用soap UI (我测试使用的版本 5.2.1) ...

  8. 【c++错误】类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)

    今天编写类的程序的时候不小心把类后的分号忘写了,就出现上面的错误提示. 顺便复习下类的正确格式: class 类名 { public: //习惯上将公有类型放在前面,便于阅读 ……(外部接口) pro ...

  9. Activation error occured while trying to get instance of type Database,key ""之Oracle

    我在发布web项目时好几次好遇到这个问题,查看了别人的说法,感觉还是不能解决,后来发现在发布时bin里面有dll没有打包到发布文件的bin目录中,而这些dll又是在连接Oracle(我选择的Oracl ...

随机推荐

  1. python3.0笔记

    python文件头 #!/usr/bin/env python # -*- coding: utf- -*- ''' Created on 2017年5月9日 @author: Administrat ...

  2. 在Ubuntu16.04下安装SourceInsight和WeChat

    1 使用Wine安装SourceInsight4 1.1 安装Wine $ sudo apt-get install wine 1.2 安装SourceInsight 下载SourceInsight软 ...

  3. Linux内核调试方法总结之反汇编

    Linux反汇编调试方法 Linux内核模块或者应用程序经常因为各种各样的原因而崩溃,一般情况下都会打印函数调用栈信息,那么,这种情况下,我们怎么去定位问题呢?本文档介绍了一种反汇编的方法辅助定位此类 ...

  4. charles_02_模拟弱网测试

    前言 用户使用app的场景是多变的,不一定稳定在WiFi或者4G网络下.大多数用户会在地铁.电梯等弱网情况下使用app,这些弱网情况下app常会出现一些数据丢失.闪退.页面展示不友好等情况.在测试过程 ...

  5. jmeter逻辑控制器详解(2)

    逻辑控制器 8.Runtime Controller 运行周期控制器,顾名思义,这是一种设置运行时间的控制器,它的效果就是使该控制器下的子项运行时间为[Runtime]中的数值(单位:s). Runt ...

  6. 解决sql语句中参数为空(null)不会更新参数的问题

    用的mybatis自动生成的 情景: 修改页面中,修改某个字段,修改前有数据,修改后为空. mybatis中一般用到 如:(这种直接忽略为空的字段,不能更新空字段参数) <update id=& ...

  7. jQ全选或取消全选

    function checkAll(chkobj) {        if ($(chkobj).children("span").text() == "全选" ...

  8. 腾讯视频的手机端的地址和PC端的地址是不一样的

    腾讯视频的手机端的地址和PC端的地址是不一样的,所以使用iframe的时候记得要使用手机端的地址

  9. Mac入门--通过Homebrew安装PHP(新)

    1 首先安装homebrew,安装过的话更新 安装:homebrew官网地址:https://brew.sh/index_zh-cn.html.或者直接复制下面代码: /usr/bin/ruby -e ...

  10. PHP 中的 $this, static , self ,parent 等等关键字的总结

    先说结论: self 和 __CLASS__,都是对当前类的静态引用,取决于定义当前方法所在的类.也就是说,self 写在哪个类里面, 它引用的就是谁.$this 指向的是实际调用时的对象,也就是说, ...