我有以下4个文件:
arrayListType.h:声明并定义arrayListType类作为模板 unorderedArrayListType.h:继承自arrayListTypeclass和Declares并定义unorderedArrayListType为模板. main1.cpp:测试程序测试unorderedArrayListType类. Makefile我得到一个编译错误的访问受保护的变量时说,arrayListType在unorderedArrayListType举例说:"在此范围内未声明的长度",其中长度和列表在保护变量"在此范围内不宣布名单" 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) 我正在尝试编写定义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)