gpt4 book ai didi

c++ - GCC 看不到通过多重继承实现

转载 作者:行者123 更新时间:2023-11-28 07:08:43 27 4
gpt4 key购买 nike

我正在尝试使用基于策略的设计来概括我的类,但 gcc 似乎没有看到在基类中实现的纯虚函数的实现。这是一个例子:

#include <iostream>

template <typename ReturnValue, template <typename> class... AccessPolicies>
struct testInterface : public AccessPolicies< ReturnValue >::interface...
{

};

template <typename DataClass, typename ReturnValue, template <typename> class... AccessPolicies>
struct testImplementation : public DataClass,
public testInterface< ReturnValue, AccessPolicies... >,
public AccessPolicies< ReturnValue >::template implementation< DataClass >...
{

};

template < typename ReturnValue >
struct GetByIndex;

template <>
struct GetByIndex< std::string >
{
class interface
{
public:
virtual std::string operator[](size_t ) = 0;
protected:
virtual ~interface() = default;
};

template <class DataClass>
class implementation
{
public:
virtual std::string operator[](size_t )
{
return "test by index";
}
protected:
virtual ~implementation() = default;
};
};

template < typename ReturnValue >
struct GetByName;

template <>
struct GetByName< std::string >
{
class interface
{
public:
virtual std::string operator[](std::string ) = 0;
protected:
virtual ~interface() = default;
};

template <class DataClass>
class implementation
{
public:
virtual std::string operator[](std::string )
{
return "test by string";
}
protected:
virtual ~implementation() = default;
};
};

struct data
{

};

int main()
{
testImplementation< data, std::string, GetByIndex, GetByName> test;
testInterface< std::string, GetByIndex, GetByName >& Test = test;

std::cout << Test[5] << std::endl;

return 0;
}

我遇到的错误是:

..\nienazwany\main.cpp: In function 'int main()':
..\nienazwany\main.cpp:78:67: error: cannot declare variable 'test' to be of abstract type 'testImplementation<data, std::basic_string<char>, GetByIndex, GetByName>'
testImplementation< data, std::string, GetByIndex, GetByName> test;
^
..\nienazwany\main.cpp:10:8: note: because the following virtual functions are pure within 'testImplementation<data, std::basic_string<char>, GetByIndex, GetByName>':
struct testImplementation : public DataClass,
^
..\nienazwany\main.cpp:53:29: note: virtual std::string GetByName<std::basic_string<char> >::interface::operator[](std::string)
virtual std::string operator[](std::string ) = 0;
^
..\nienazwany\main.cpp:26:29: note: virtual std::string GetByIndex<std::basic_string<char> >::interface::operator[](size_t)
virtual std::string operator[](size_t ) = 0;
^
..\nienazwany\main.cpp:81:24: error: request for member 'operator[]' is ambiguous
std::cout << Test[5] << std::endl;
^
..\nienazwany\main.cpp:53:29: note: candidates are: virtual std::string GetByName<std::basic_string<char> >::interface::operator[](std::string)
virtual std::string operator[](std::string ) = 0;
^
..\nienazwany\main.cpp:26:29: note: virtual std::string GetByIndex<std::basic_string<char> >::interface::operator[](size_t)
virtual std::string operator[](size_t ) = 0;

有两个问题我不太明白:

  1. 编译器似乎没有考虑AccessPolicy< ReturnType >::implementation< DataClass >...成为 AccessPolicy< ReturnType >::interface... 的实现即使函数签名完全相同。
  2. 编译器无法解析我调用的是哪个 operator[],即使它们都有不同的参数,我显然调用了 size_t 一个(数字不能隐式转换为字符串)。

知道为什么会这样吗?

我的猜测是,即使我直接从“接口(interface)”和“实现”继承,成员函数也会以某种方式在不同的 namespace 中结束。如果那是正确的,我该如何解决这个问题?


编辑:根据要求添加了去除模板的上述示例

#include <iostream>

class GetByIndexInterface
{
public:
virtual std::string operator[](size_t ) = 0;
protected:
virtual ~GetByIndexInterface() = default;
};

class GetByIndexImplementation
{
public:
virtual std::string operator[](size_t )
{
return "test by index";
}
protected:
virtual ~GetByIndexImplementation() = default;
};


class GetByNameInterface
{
public:
virtual std::string operator[](std::string ) = 0;
protected:
virtual ~GetByNameInterface() = default;
};

class GetByNameImplementation
{
public:
virtual std::string operator[](std::string )
{
return "test by string";
}
protected:
virtual ~GetByNameImplementation() = default;
};

struct data
{

};

struct testInterface : public GetByIndexInterface,
public GetByNameInterface
{

};

struct testImplementation : public data,
public testInterface,
public GetByIndexImplementation,
public GetByNameImplementation
{

};

int main()
{
testImplementation test;
testInterface& Test = test;

std::cout << Test[5] << std::endl;

return 0;
}

最佳答案

你是 struct testImplementation 继承自 struct testInterface 本身继承自 struct GetByNameInterface 定义了一个 virtual std::string运算符[](std::string) = 0;

testInterfacetestImplementation 都没有为此虚拟定义覆盖,因此 testImplementation 是一个抽象结构。

您从与前一个类没有关系但定义了相同的 operator 的另一个类继承这一事实对您没有帮助。您必须在具体实现和抽象接口(interface)之间的层次结构中实现您的方法,而不是在辅助类上。

关于c++ - GCC 看不到通过多重继承实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361763/

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