相关疑难解决方法(0)

templates:父类成员变量在继承的类中不可见

我有以下4个文件:

  1. arrayListType.h:声明并定义arrayListType类作为模板
  2. unorderedArrayListType.h:继承自arrayListTypeclass和Declares并定义unorderedArrayListType为模板.
  3. main1.cpp:测试程序测试unorderedArrayListType类.
  4. Makefile

我得到一个编译错误的访问受保护的变量时说,arrayListTypeunorderedArrayListType举例说:"在此范围内未声明的长度",其中长度和列表在保护变量"在此范围内不宣布名单" arrayListType类.

以下是代码:
arrayListType.h

#ifndef H_arrayListType  
#define H_arrayListType

#include <iostream>

using namespace std;

template <class elemType>
class arrayListType
{

public:

    const arrayListType<elemType>&operator=(const arrayListType<elemType>&);

    bool isEmpty() const;
    bool isFull() const;
    int listSize() const;
    int maxListSize() const;
    void print() const;
    bool isItemAtEqual(int location, const elemType& item) const;
    virtual void insertAt(int location, const elemType& insertItem) = 0;
    virtual void insertEnd(const elemType& insertItem) …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates makefile class

37
推荐指数
3
解决办法
2万
查看次数

C++找不到继承自当前模板类的模板基类中定义的类型

我正在尝试编写定义super类型习惯用法模板类的变体.该类Inherit引入了类型Super来表示可能非常长的超类型,并且还需要知道派生类型New来做一些我没有在这里展示的额外的东西.

如果传递给的类型New不是模板,但是模板失败,则此方法正常.这是一个用clang++-3.8 -std=c++1y -Wall(gcc给出相同的输出)编译的完整示例:

struct SomeBase {};

template<class New, class Base>
struct Inherit : Base {
    using Super = Inherit<New, Base>;
};

struct NonTemplate : Inherit<NonTemplate, SomeBase> {
    using X = Super;
    // compiles and is nice and short
};

template<class T>
struct Template : Inherit<Template<T>, SomeBase>
{
    using A = Super;
    // error: unknown type name 'Super'; did you mean 'NonTemplate::Super'?

    using B …
Run Code Online (Sandbox Code Playgroud)

c++ templates super crtp typename

2
推荐指数
1
解决办法
671
查看次数

标签 统计

c++ ×2

templates ×2

class ×1

crtp ×1

inheritance ×1

makefile ×1

super ×1

typename ×1