【问题标题】:g++ compiler doesnt recognize nested template class [duplicate]g ++编译器无法识别嵌套模板类[重复]
【发布时间】:2013-06-06 17:58:27
【问题描述】:

我有一个模板数组类,该数组在内存中有连续数量的单元格。该数组还有一个迭代器=> 迭代器和单元格是 Array 中的嵌套类,该类在此代码中进行了描述:

#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 &lt;&lt; 时遇到问题。
在 Visual Studio 2012 中尝试:

Array<int> a;
a.add(3);
cout<<a;

它的输出和往常一样是 3,但在 g++ 中,编译器无法识别 operator &lt;&lt; 实现的第二行中的 Iterator 类。

这是operator &lt;&lt;的实现代码(记住是友元函数)

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&lt;G&gt;::Iterator 是从属名称。您需要使用typename 消歧器
  • 改成'typename',还是一样的错误...
  • @AviadChmelnik 将什么更改为typename?你的意思是你添加了typename 并且仍然得到错误?固定行应为typename Array&lt;G&gt;::Iterator itr ...
  • 现在清楚了!!!非常感谢!

标签: c++ templates compiler-errors g++


【解决方案1】:

将通过更改行来修复:

Array<G>::Iterator itr=a.begin(),end=a.end();

到:

typename Array<G>::Iterator itr=a.begin(),end=a.end();

感谢 Andy Prowl 和 Praetorian

【讨论】:

  • 当我早些时候遇到同样的问题时,这对我来说很好。
猜你喜欢
  • 2020-11-13
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 2018-10-28
  • 2017-11-29
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多