首页 > 解决方案 > 我的超类中带有结构定义的模板问题

问题描述

我有个问题。我的基类“ABC”有两个模板类 A 和 B。我的类“superABC”继承了 ABC,但第二个模板固定在字符串上。在 ABC 中有一个名为“structABC”的结构。在 superABC 中是一个从 structABC 返回实例的函数。当我尝试编写这个函数的实现时,编译器给了我

C2244 错误“无法将函数定义与现有声明匹配”。

有人可以告诉我问题是什么吗?

//ABC.h
#pragma once
template<class A, class B>
class ABC
{
public:
    void func();
    struct structABC {

    };

    structABC _referenceRange;
};

//superABC.h
#pragma once
#include "ABC.h"
#include "string.h"
template<class A>
class superABC:
    public ABC<A, string>
{
public:
    typename ABC<A, string>::structABC getBCD();
};

//superABC.cpp
#include "superABC.h"
template<class A>
inline typename ABC<A, string>::structABC superABC<A>::getBCD()
{
    return ABC<A, string>::structABC();
}

标签: c++templatesstruct

解决方案


对我来说看起来像 MSVC 错误;原始代码使用 clang 编译。作为一种解决方法,添加一个 typedef for ABC<A, string>::structABC,如下所示:

template<class A>
class superABC : public ABC<A, string>
{
    using structABC = typename ABC<A, string>::structABC;
public:
    structABC getBCD();
};

template<class A>
typename superABC<A>::structABC superABC<A>::getBCD()
{
    return structABC();
}

演示


推荐阅读