我有以下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) 我遇到了与上一个问题相同的问题: 在C++中使用未声明的标识符和模板以及继承
总结一下,我们尝试从子类访问模板类的protected属性.所描述的方法是使用this->attribute而不是仅使用attribute.问题是,我想知道为什么visual studio 2012不需要在程序的变量引用前添加this->来正确编译和执行.我还想知道是否有办法在OS X上的gcc或其他编译器中使用该功能.
编辑:这是我用于在visual studio 2012中测试的代码.
//file a.h
template<class T>
class a
{
public:
a(){value = 2;};
protected:
T value;
};
template<class T>
class b: public a<T>
{
public:
T getValue(){return value;};
};
//file main.cpp
#include <iostream>
#include "a.h"
using namespace std;
int main()
{
b<int> myTest;
cout<<myTest.getValue();
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不是使用g ++编译,而是使用visual studio 2012.