У меня есть массив массивов шаблонов, массив имеет количество элементов в памяти. Кроме того, массив имеет итератор => итератор и Cells вложенные классы в массиве класс описан в этом коде:g ++ компилятор не распознает класс вложенных шаблонов
#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, как обычно, но в г ++ компилятор не может распознать класс итератора во 2-й линии реализации operator <<
.
Вот код реализации 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'
'Массив :: Итератор является зависимым именем. Вам нужно использовать disambiguator 'typename' –
, чтобы перейти к 'typename', по той же ошибке ... –
@AviadChmelnik Изменено как' typename'? Вы имеете в виду, что вы добавили 'typename' и все еще получаете ошибку? Фиксированная строка должна читать 'typename Array :: Iterator itr ...'. –
Praetorian