You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从C++模板抽象类dictionary继承,寻求技术解决方案

解决继承模板抽象类时的"expected class-name before '{' token"错误

编译错误的核心原因是继承模板类时未指定模板参数。dictionary是一个类模板,而非具体类,编译器无法仅凭dictionary这个名称确定要继承的具体类型。

修改MyDictionary.h中的继承声明,为dictionary添加模板参数即可解决问题:

#ifndef UNTITLED_CPP_MYDICTIONARY_H
#define UNTITLED_CPP_MYDICTIONARY_H

#include "dictionary.h"

template<class Key, class Value>
// 补充模板参数<Key, Value>,明确继承的是对应模板实例
class MyDictionary : public dictionary<Key, Value> {
public:
    const Value &get(const Key &key) const;
    void set(const Key &key, const Value &value);
    bool is_set(const Key &key) const;

    ~MyDictionary() = default;
};


#endif //UNTITLED_CPP_MYDICTIONARY_H

补充说明

类模板本身不是可直接使用的类,只有为它提供具体的模板参数(比如int、std::string,或者这里的模板参数Key/Value)后,编译器才会生成对应的具体类。继承模板类时必须显式指定模板参数,否则编译器无法识别要继承的目标类型,从而抛出expected class-name before '{' token错误。

内容的提问来源于stack exchange,提问作者Анна

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 00:57:23