g++编译器不能识别嵌套模板类
g++ compiler doesnt recognize nested template class
我有一个模板数组类,该数组在内存中有连续的单元格。数组也有一个iterator=> iterator, Cells是数组中的嵌套类,该类在下面的代码中描述:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include "sort.h"
using namespace std;
//Array is array of T's (template objects)
template <class T>
class Array
{
private:
//the array is consist cellss that holds the data
template<class S>
class Cell
{
public:
//members:
S* m_data;
//methods:
//C'tor:(inline)
Cell(S* data=NULL): m_data(data){};
//D'tor:(inline)
~Cell(){delete m_data;};
//C.C'tor:(inlnie)
Cell(const Cell<S>& cell): m_data(cell.m_data){};
};
public:
//the Array may has an iterator:
class Iterator
{
public:
//members:
Cell<T>* m_current;
//default C'tor:(inline)
Iterator(Cell<T>* ptr=NULL):m_current(ptr){};
//C.C'tor:(inline)
Iterator(const Iterator& itr2):m_current(itr2.m_current){};
//D'tor:(inline)
~Iterator(){};
//////Operators/////
//assigment operator:
Iterator& operator = (const Iterator& itr2){m_current=itr2.m_current;
return *this;};
//comparsion operators:
bool operator == (const Iterator& itr2)const
{return (itr2.m_current==m_current);};
bool operator != (const Iterator& itr2) const{return !(*this==itr2);};
//reference operator:
T& operator * ()const {return *(m_current->m_data);} ;
//forward operators (++):
Iterator& operator ++ () // prefix: ++a
{m_current=&(m_current[1]);
return *this;};
const Iterator operator ++ (int)// postfix: a++
{Iterator itr=*this;
++*this;
return itr;};
private:
//members of Array:
Cell<T>* m_head,*m_last;
unsigned int m_size;
public:
/*******************C'tors and D'tors************************/
//C'tor:(inline)
Array():m_head(NULL),m_last(NULL), m_size(0){};
//D'tor:
~Array(){delete[] m_head;};
//C.C'tor:
Array(const Array& array): m_head(array.m_head),m_last(array.m_last),m_size(array.m_size){};
/****************Adding********************/
//add an element to the end of the Array:
void add(const T added);
/*********************Iterate*****************************/
//return an iterator to the start of the Array:
Iterator begin() const {return Iterator(m_head); };
//return an iterator to the element after the end of the Array:
Iterator end() const{return Iterator(&(m_last[1]));};
/*****************************Operators********************************/
//printing all the elements in the Array (with a specific format)
template <typename G> friend std::ostream& operator << (ostream &os, const Array<G> &a);
};
现在我在尝试实现operator <<
时遇到了一个问题。
在Visual Studio 2012中尝试:
Array<int> a;
a.add(3);
cout<<a;
它的输出通常是3,但在g++中,编译器不能识别operator <<
实现的第二行中的Iterator类。
下面是operator <<
的实现代码(记住它是好友函数)
template<class G>std::ostream& operator << (ostream &os,const Array<G> &a)
{
//crtating traversal and bound:
Array<G>::Iterator itr=a.begin(),end=a.end();
//traverse and print:
while (itr!=end)
{
os<<*itr++;
//last element should not print space!:
if (itr!=end)
cout<<" ";
}
return os;
}
我在这里错过了什么?我需要把另一个template
放在某个地方吗?我得到的错误是:
Array.h: In function 'std::ostream& operator<<(std::ostream&, const Array<G>&)':
Array.h:296: error: expected ';' before 'itr'
修改行:
Array<G>::Iterator itr=a.begin(),end=a.end();
:
typename Array<G>::Iterator itr=a.begin(),end=a.end();
感谢Andy Prowl和Praetorian
相关文章:
- 嵌套在类中时无法设置成员数据
- 无法访问嵌套类.类的使用无效
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在C++中搜索嵌套多映射值
- 在C++中将矢量转换为嵌套地图
- C++嵌套if语句,基本货币交换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 嵌套的匿名命名空间
- 了解嵌套循环打印星号图案
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 嵌套for循环C++的问题(初学者)
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 用C#中的并集模拟C++嵌套结构
- 部分专业化和嵌套模板
- 嵌套While循环不起作用(C++问题)
- C++-模板嵌套类的引用初始化无效
- 嵌套成员类型识别
- g++编译器不能识别嵌套模板类