operator-overloading of nested class inside a template class

Discussion in 'C++' started by Gerhard Pfeiffer, Sep 14, 2006.

  1. Hi,

    I'm trying to implement a data-structure and have an iterator for it. Now I've
    got a problem impleneting the operator+. I tried to isolate the problem:

    template<int DIM, typename T> class data_structure {
    private:
    int data[256];
    public:
    class iterator {
    public:
    int index;
    };
    };

    template<int DIM, typename T>
    data_structure<DIM, T>::iterator
    operator+(data_structure<DIM, T>::iterator it, int n) {
    iterator ret();
    ret.index = it.index + n;
    }

    and g++ gives me this error:
    bla.cpp:13: error: expected constructor, destructor, or type conversion before
    'operator'

    I just don't see, what I'm doing wrong here. Any hints would be appreciated.

    Ciao,
    Gerhard
     
    Gerhard Pfeiffer, Sep 14, 2006
    #1
    1. Advertisements

  2. Gerhard Pfeiffer wrote:
    > Hi,
    >
    > I'm trying to implement a data-structure and have an iterator for it.
    > Now I've got a problem impleneting the operator+. I tried to isolate
    > the problem:
    >
    > template<int DIM, typename T> class data_structure {
    > private:
    > int data[256];
    > public:
    > class iterator {
    > public:
    > int index;
    > };
    > };
    >
    > template<int DIM, typename T>
    > data_structure<DIM, T>::iterator
    > operator+(data_structure<DIM, T>::iterator it, int n) {
    > iterator ret();

    ^^^^^^^^^^^^^^^^^^^^^^
    This is a declaration of a function. Drop the parentheses.

    > ret.index = it.index + n;
    > }
    >
    > and g++ gives me this error:
    > bla.cpp:13: error: expected constructor, destructor, or type
    > conversion before 'operator'
    >
    > I just don't see, what I'm doing wrong here. Any hints would be
    > appreciated.


    Read the FAQ. All of it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 14, 2006
    #2
    1. Advertisements

  3. Gerhard Pfeiffer

    David Harmon Guest

    On 14 Sep 2006 18:04:59 GMT in comp.lang.c++, Gerhard Pfeiffer
    <> wrote,
    >template<int DIM, typename T>
    >data_structure<DIM, T>::iterator
    >operator+(data_structure<DIM, T>::iterator it, int n) {
    > iterator ret();
    > ret.index = it.index + n;
    >}
    >


    iterator is a type dependent on the template parameter; "typename"
    is required.

    template<int DIM, typename T>
    typename data_structure<DIM, T>::iterator
    operator+(typename data_structure<DIM, T>::iterator it, int n) {
    iterator ret();
    ret.index = it.index + n;
    }
     
    David Harmon, Sep 14, 2006
    #3
  4. David Harmon wrote:
    > On 14 Sep 2006 18:04:59 GMT in comp.lang.c++, Gerhard Pfeiffer
    > <> wrote,
    >> template<int DIM, typename T>
    >> data_structure<DIM, T>::iterator
    >> operator+(data_structure<DIM, T>::iterator it, int n) {
    >> iterator ret();
    >> ret.index = it.index + n;
    >> }
    >>

    >
    > iterator is a type dependent on the template parameter; "typename"
    > is required.
    >
    > template<int DIM, typename T>
    > typename data_structure<DIM, T>::iterator
    > operator+(typename data_structure<DIM, T>::iterator it, int n) {
    > iterator ret();


    'ret' is still a function here...

    > ret.index = it.index + n;
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 14, 2006
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    template overloading / nested class

    Alexander Stippler, Jul 24, 2003, in forum: C++
    Replies:
    3
    Views:
    394
    Rob Williscroft
    Jul 24, 2003
  2. Replies:
    5
    Views:
    631
  3. Ares Lagae
    Replies:
    2
    Views:
    393
    James Kanze
    Oct 20, 2008
  4. claudiu
    Replies:
    2
    Views:
    332
    claudiu
    Apr 1, 2011
  5. claudiu
    Replies:
    3
    Views:
    523
    claudiu
    Apr 1, 2011
Loading...

Share This Page