gpt4 book ai didi

c++ - 具有结构定义的我的父类(super class)中的模板问题

转载 作者:行者123 更新时间:2023-12-02 10:36:30 25 4
gpt4 key购买 nike

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

C2244 error "unable to match function definition to an existing declaration".



有人可以告诉我问题是什么吗?
//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();
}

最佳答案

对我来说似乎是MSVC的错误;原始代码使用clang编译。解决方法是,为ABC<A, string>::structABC添加typedef,如下所示:

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();
}

Demo

关于c++ - 具有结构定义的我的父类(super class)中的模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60060187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com